When a shopping cart is checked out, you need to save an order into the database. Orders contain information about customers and the products they are buying. For this, you need to register customer orders. This video shows how to achieve this.
- [Narrator] Let's move on to the sixth video of this section, registering customer orders. In the previous video, we learned how to create a context processor for the current cart. In this video, we're going to take a look at registering customer orders. We'll first create order models and then include order models in an administration site and finally create customer orders. When a shopping cart is checked out, you need to save an order into a database. Orders will contain information about customers and the products they are buying. Create a new application for managing customer orders using the following command: python manage.py startapp orders.
Now edit the settings.py file of your project and add orders to the installed app setting as we do here. You've activated the new application. Next we create order models. You need a model to store the order details and a sound model to store items bought, including their price and quantity. Edit the models.py file of the orders application and add these lines of code to it and save.
The order model contains several fields for custom information in a paid Boolean field, which defaults to false. Later on, we're going to use this field to differentiate between paid and unpaid orders. We also define a get total cost method to obtain the total cost of the items bought in this order. The order item model allows us to store the product, quantity and price paid for each item. We include get cost to return the cost of the item. Run the python manage.py makemigrations command to create initial migrations for the orders application.
Once you run this command, you'll see this output. Migrations for orders, the file name, create model order and create model order item. Now run python manage.py migrate command to apply the new migration. Your models are now synced to the database. Let's include all the models in an administration site. Edit the main .py file of the orders application.
Add these lines of code for model order and order item and save it. We use a model inline for the order item model to include it as an inline in the order admin class. An inline allows you to include a model for appearing on the same edit page as the parent model. Run the development server with the command python manage.py runserver. And then open the browser and enter this URL. So you can now see this page. Next we create customer orders.
We need to use the order models that we just created to persist the items contained in the shopping cart when the user finally wishes to place an order. The functionality for creating a new order will work in these steps. First, we present users an order form to fill in their data. Next, we create a new order instance with the data entered by the users, and then we create an associated order item instance for each item in the cart. And finally, we clear all the cart contents and redirect the users to a success page. So first, we need a form to enter order details.
Create a new file inside the orders application directory and name it forms.py. Then add these lines of code to it and save. This is the form that we're gonna use for creating new order objects. Now we need a view to handle the form and create a new order. Edit the views.py file of the orders application and add these lines of code to it. In the order create view, we'll obtain the current cart from the session with cart = Cart(request). Depending on the request method, we perform these tasks.
The first is the get requests, which instantiates the order create form form and renders the template orders/order/created.html. Next is the post request. This validates the data that got posted. If the data is valid, we'll use order = form.save method to create a new order instance. We'll then save it to a database and store it in the order variable. After creating the order, we alliterate over the cart items and create OrderItem for each of them. Finally, we clear the cart contents.
Now create a new file inside the orders application directory and name it urls.py. Add the code for URL pattern to it and save it. This is the URL pattern for the order_create view. Edit the urls.py file of My Shop and include this pattern. Remember to place it before the shop.urls pattern. Now edit the detail.html template of the cart application and replace the line href = "#" with this line of code.
Users can now navigate from the cart detail page to the order form. We still need to define templates for placing orders. So create a proper file structure inside the orders application as we do here. Inside the templates folder, create an orders folder which contains another folder name, order. And here we place the file create.html. Now edit create.html template and include these code and save it.
The template displays the cart items including totals and the form to place an order. Now create a new file and name it created.html. And add these lines of code and save. This is the template that we render when the order is successfully created. Start the web development server to track new files. Open http://127001:8000 in your browser. Add a couple of products to the cart and continue to the checkout page.
You can see your page here. Fill in the form with the valid data. So add your name, your last name, email ID, address, postal code and city. Now click on Place Order button. The order will be created and now you can see a success page like this one. Wonderful. In this video, we successfully registered customer orders. In the next video, we'll learn how to launch asynchronous tasks with Celery.
Note: This course was created by Packt Publishing. We are pleased to host this training in our library.
- Creating an online shop project
- Registering customer orders
- Managing payments and orders
- Integrating a payment gateway
- Extending the online shop
- Creating a coupon system
- Translating Python code