This video shows how to create a context processor which enhances the efficiency of your shopping cart and provides a better user interaction.
- [Instructor] This is the fifth video of this section, Creating a Context Processor for the Current Cart. In the previous video, we learned to create shopping cart views for our online shop. In this video, we're going to take a look at context processors and then we'll set the cart into the request context. We begin by creating a context processor for the current cart. You might have noticed that we still are showing the message "Your cart is empty" in the header of the site. When we start adding items to the cart, you'll see that the total number of items in the cart and the total cost instead.
Since this is something that should be displayed in all the pages, we'll build a context processor to include the current cart in the requests context regardless of the view that is being processed. A context processor is a Python function that takes the request object as an argument and returns a dictionary that gets added to the request context. They come in handy when you need to make something available to all templates. By default, when you create a new project using the start project command, your project will contain the following template context processors in the context_processors option inside the template setting.
First is django.template.context_processors.debug. This sets the boolean debug and SQL queries variables in the context representing the list of SQL queries executed in the request. Next is django.template.context_processors.request which sets the requester variable in the context. Then comes django.contrib.auth.context_processors.auth. This sets the user variable in the request. The last one is django.contrib.messages.context_processors.messages which is used to set a messages variable in the context containing all messages that have been sent using the messages framework.
Django also enables django.template.context_processors.csrf to avoid cross-site request forgery attacks. This context processor is not present in the settings, but it's always enabled and cannot be turned off for security reasons. You can see the list of all built in context processors at this website. Let's see how to set the cart in to the request context. We create a context processor to set the current cart into the request context for templates. We'll be able to access this cart in any template.
Create a new file in the cart application directory and name it context_processors.py. Context processors can reside anywhere in your code, but creating them here will keep your code well organized. Now, add the request to the cart method and return it. As you can see, a context processor is a function that receives a request object as a parameter and returns a dictionary of objects that will be available to all templates rendered using request context. In our context processor, we instantiate the cart using the request object and make it available for the templates as a variable named 'cart.' Then we edit these settings .py file of your project and add cart.context_processors.cart to the context processor option inside the template setting and save the file.
Your context processor will now be executed every time a template is rendered using django's request context. The cart variable will be set in the context for your templates. Context processors are executed in all of the requests that use request context. You might want to create a custom template tag instead of a context processor if you're going to access the database. Now, edit the shop/base.html template of the shop application and find the div tag.
Reload your server using the command python manage.py runserver. Open http://127.0.0.1:8000 in your browser and add some products to the cart. In the header of the website, you can see the total number of items in the current and the total cost in the same way as it's displayed here as $80. Superb. In this video we have created a context processor for the current cart. In the next video, we'll have a look at Registering Customer Orders.
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