Creating customer orders

We will use the order models we created to persist the items contained in the shopping cart when the user finally places an order. A new order will be created following these steps:

  1. Present users an order form to fill in their data
  2. Create a new Order instance with the data entered, and create an associated OrderItem instance for each item in the cart
  3. Clear all the cart content and redirect users to a success page

First, we need a form to enter the order details. Create a new file inside the orders application directory and name it forms.py. Add the following code to it:

from django import formsfrom .models import Orderclass OrderCreateForm(forms.ModelForm):    class Meta:        model = Order fields = ['first_name', 'last_name', ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.