This tutorial is the second in a series on building ** e-commerce ** apps using the ** Django ** web framework. If you missed the first tutorial, you can find it online here. How to develop web applications on Alibaba Cloud using the Django framework (https://www.alibabacloud.com/blog/how-to-develop-web-applications-on-alibaba-cloud-with-django) -framework_594265? spm = a2c65.11461447.0.0.25d054951cPDkx)
With the advent of new data and technology products, every business now needs to build its own e-commerce platform and integrate it directly into the company's website. This tutorial is intended to show you how to harness the power of the Django framework to build a cart system.
** 2.1. Project details ** The project we are building is an e-commerce application that allows people to buy and sell goods. For flexibility and maintainability, the project is divided into three independent apps: Core app (developed in previous tutorial), Cart app (developed in this tutorial), Payment app (developed in next tutorial) ..
** 2.2. Cart application ** The cart application is used to manage the purchasing and selling process, and to manage the cart itself and the cart items in the cart (adding, updating, deleting cart items).
The specific elements managed here are:
--Purchase and sale options --Client carts and cart items --Add, update, or delete items in your cart
** 2.3. Development environment settings ** We start by getting the source code for a project that is already online.
--Clone the project using GIT.
$ cd ~/projects # Switch to your project folder on Home directory
$ git clone https://github.com/binel01/buyandsell.git
Note: After issuing this command, a folder named buyandsell will be created in your project directory to contain all the source code for your project.
--Make sure you have all the required packages installed. Python3.6, Pip, Virtualenv, libpq-dev, postgresql, postgresql-contrib --Install virtualenv
$ pip install virtualenv
--Create venv
$ virtualenv venv
--Activate venv
$ source venv\bin\activate
--Dependency installation
(venv)$ pip install -r requirements.txt
--Database initialization
(venv)$ python manage.py migrate
--Create a superuser
(venv)$ python manage.py createsuperuser # then follow the instructions
--Test if everything is working.
(venv)$ python manage.py runserver
We will proceed with this flow.
--App initialization -** Define the database model in the models.py ** file. -** Defines the views that handle the request in views.py **. -Use the ** urls.py ** file to define a route for users to navigate your application. -The ** admin.py ** file is used to define settings for the administrator interface to help application administrators manage entities stored in the database. ――After that, customize the template to make it look more cute.
First, you need to create a new application called ** cart ** and add it to the ** settings.py ** configuration file.
(venv)$ django-admin startapp cart
This command creates a new folder named cart that contains the source code for the cart application.
Add the cart app to the settings.py file.
...
INSTALLED_APPS = [
...
cart,
...
]
...
As mentioned above, this app supports the following models.
from django.db import models
from django.contrib.auth.models import User
from datetime import datetime
from core.models import Product
class 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.1. View ** In order to manage the data presented to the user, we have to define a view that acts as our control.
Below are the ** views.py ** files that result from the ** Cart ** and ** CartItem ** database models.
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'
Here is the URL root defined in buyandsell / cart / urls.py.
from django.urls import path, include
from . 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'),
]
In general, when you build a web application to solve your client's business needs, you also build an administrator application that manages the data, permissions, permissions, and roles stored in the database. Django simplifies the life of web developers because this is already done by default.
To configure the admin interface, you must modify the ** admin.py ** file to configure it to use the model.
The resulting ** admin.py ** file is:
from django.contrib import admin
# Register your models here.
class CartAdmin(admin.ModelAdmin):
pass
class CartItemAdmin(admin.ModelAdmin):
pass
(venv)$ cd buyansell
(venv)$ python manage.py runserver
Then move your browser to this location.
http://localhost:8000
By the end of this tutorial, you should know how to get started building your application with the Django framework. More precisely, you've learned what Django's models, views, and templates are.
The source code for the application used in this tutorial is on the GitHub page [https://github.com/binel01/buyandsell/tree/master/core](https://github.com/binel01/buyandsell/tree/master/ It is located at core? spm = a2c65.11461447.0.0.25d054951cPDkx).
The next part of the tutorial in this series will show you how to develop a cart application to manage product sales.
Recommended Posts