Develop a Cart Application with Django

1. Introduction

2. Scope of Our Project

2.1. Project Details

2.2. The Cart Application

  • Buy and Sell options
  • Cart and CartItems of the client
  • Adding, updating and removing Products to the Cart

2.3. Setting Up the Development Environment

  • We clone the project using GIT
$ cd ~/projects                 # Switch to your project folder on Home directory
$ git clone https://github.com/binel01/buyandsell.git
  • make sure all the required packages are installed (Take a look at the previous tutorial setup tools section): Python3.6, Pip, Virtualenv, libpq-dev, postgresql, postgresql-contrib
  • Install virtualenv
$ pip install virtualenv
  • Create a venv
$ virtualenv venv
  • Activate the venv
$ source venv\bin\activate
  • Install the dependencies
(venv)$ pip install -r requirements.txt
  • Initialize the database
(venv)$ python manage.py migrate
  • Create a super user
(venv)$ python manage.py createsuperuser  # then follow the instructions
  • Test if everything is working well
(venv)$ python manage.py runserver

3. Develop the Cart App

  • Initialize the app
  • Define the database models inside the models.py file
  • Define the views that will handled requests inside views.py
  • Define the routes that users will follow to navigate through our applications using urls.py file.
  • Define the admin interface configurations that will help the application administrator to effortlessly manage the entities stored on the database using admin.py file
  • We will then customize our templates to look prettier

3.1. Initialize the App

(venv)$ django-admin startapp cart
...
INSTALLED_APPS = [
...
cart,
...
]
...

3.2. Defining the Models

  • Cart
  • CartItem
from django.db import models
from django.contrib.auth.models import User
from datetime import datetime
from core.models import Productclass Cart(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
created_at = models.DateTimeField(default=datetime.now)
class CartItem(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.IntegerField(default=1)
price_ht = models.FloatField(blank=True)
cart = models.ForeignKey('Cart', on_delete=models.CASCADE)
TAX_AMOUNT = 19.25 def price_ttc(self):
return self.price_ht * (1 + TAX_AMOUNT/100.0)
def __str__(self):
return self.client + " - " + self.product

3.3. Defining the Views

3.3.1. The Views

from django.shortcuts import render
from django.views.generic import DetailView, ListView, CreateView, UpdateView, DeleteView
from .models import Cart, CartItem##-------------- Cart Views --------------------------------------
class DetailCart(DetailView):
model = Cart
template_name='cart/detail_cart.html'
class ListCart(ListView):
model = Cart
context_object_name = 'carts'
template_name='cart/list_carts.html'
class CreateCart(CreateView):
model = Cart
template_name = 'cart/create_cart.html'
class Updatecart(UpdateView):
model = Cart
template_name = 'cart/update_cart.html'
class DeleteCart(DeleteView):
model = Cart
template_name = 'cart/delete_cart.html'
##-------------- CartItem Views --------------------------------------
class DetailCartItem(DetailView):
model = CartItem
template_name='cartitem/detail_cartitem.html'
class ListCartItem(ListView):
model = CartItem
context_object_name = 'cartitems'
template_name='cartitem/list_cartitems.html'
class CreateItemCart(CreateView):
model = CartItem
template_name = 'cartitem/create_cartitem.html'
class UpdateCartItem(UpdateView):
model = CartItem
template_name = 'cartitem/update_cartitem.html'
class DeleteCartItem(DeleteView):
model = Cart
template_name = 'cartitem/delete_cartitem.html'

3.4. Defining the Url Routes

from django.urls import path, includefrom . import views# Cart Urls
urlpatterns = [
path('cart/', views.ListCart, name='list-carts'),
path('cart/<int:pk>/', views.DetailCart.as_view(), name='detail-cart'),
path('cart/create/', views.CreateCart.as_view(), name='create-cart'),
path('cart/<int:pk>/update/', views.Updatecart.as_view(), name='update-cart'),
path('cart/<int:pk>/delete/', views.DeleteCart.as_view(), name='delete-cart'),
]
# CartItem Urls
urlpatterns += [
path('cartitem/', views.ListCartItem.as_view(), name='list-cartitem'),
path('cartitem/<int:pk>/', views.DetailCartItem.as_view(), name='detail-cartitem'),
path('cartitem/create/', views.CreateCartItem.as_view(), name='create-cartitem'),
path('cartitem/<int:pk>/update/', views.UpdateCartItem.as_view(), name='update-cartitem'),
path('cartitem/<int:pk>/delete/', views.DeleteCartItem.as_view(), name='delete-cartitem'),
]

3.5. Define Admin Interface

from django.contrib import admin# Register your models here.
class CartAdmin(admin.ModelAdmin):
pass
class CartItemAdmin(admin.ModelAdmin):
pass

3.6. Testing that Everything Is Working Well

(venv)$ cd buyansell
(venv)$ python manage.py runserver
http://localhost:8000

4. Conclusion

--

--

--

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

[Technical] Documentation As Code With DacDoc

Up and Running with Alibaba Cloud Container Registry

Alibaba Cloud Savings Plans: More Flexible than Subscription and More Cost-Effective than…

Mercury Insurance login Auto, Home, Business Insurance.

Mercury Insurance  login Auto, Home, Business Insurance.

Preparing for a Technical Interview

Journey to Automated Permissions Management

Alibaba Cloud Functions on Kubernetes with Event Driven Autoscaling Ability

A complete guide to CORS!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

More from Medium

A Guide to OAuth2.0 Authorization with Django Rest Framework.

Test-Driven-Development with Django: Unit Testing & Integration testing with Docker, Flask & Github…

Django Github CI/CD

Cross-Origin Resource Sharing In Django Rest API

User Authentication — User Auth with REST API in Django Part 3