Adding items to the cart

We are getting close to finishing up the shopping cart. Now we are going to implement a view that will include items in the cart.

The first thing we need to do is create a new URL. Open the file url.py in the directory gamestore/main/, and add this URL to the urlpatterns list:

   path(r'cart/add/<int:game_id>/', views.add_to_cart),

Perfect. In this URL, we can pass the game ID, and it will execute a view called add_to_cart. Let's add this new view. Open the file views.py in gamestore/main. First, we add import statements, shown as follows:

from decimal import Decimalfrom django.shortcuts import get_object_or_404from django.contrib import messagesfrom django.contrib.auth.decorators import login_required

Now, we need ...

Get Python Programming Blueprints 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.