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:
- Present users an order form to fill in their data
- Create a new Order instance with the data entered, and create an associated OrderItem instance for each item in the cart
- 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', ...