Professional Documents
Culture Documents
How To Create A Fully Functional E-Commerce Website With Django - by Andika Pratama - Analytics Vidhya - Medium
How To Create A Fully Functional E-Commerce Website With Django - by Andika Pratama - Analytics Vidhya - Medium
71 1
https://www.shuup.com/wp-content/uploads/2017/12/python-plus-django-1.jpg
Prerequisites
Django Latest Version
Before starting the tutorial don’t forget to activate your Virtual Environment,
and the project we created in the previous tutorial. If yo not you can
download it here:
Andika7/DjangoEcommerce
Contribute to Andika7/DjangoEcommerce development by creating
an account on GitHub.
github.com
1. Import
1 CATEGORY = (
2 ('S', 'Shirt'),
3 ('SP', 'Sport Wear'),
4 ('OW', 'Out Wear')
5 )
6
7 LABEL = (
8 ('N', 'New'),
9 ('BS', 'Best Seller')
10 )
11
12 class Item(models.Model) :
13 item_name = models.CharField(max_length=100)
14 price = models.FloatField()
15 discount_price = models.FloatField(blank=True, null=True)
16 category = models.CharField(choices=CATEGORY, max_length=2)
17 label = models.CharField(choices=LABEL, max_length=2)
18 description = models.TextField()
19
20 def __str__(self):
21 return self.item_name
22
23 def get_absolute_url(self):
24 return reverse("core:product", kwargs={
25 "pk" : self.pk
26
27 })
28
29 def get_add_to_cart_url(self) :
30 return reverse("core:add-to-cart", kwargs={
31 "pk" : self.pk
32 })
33
34 def get_remove_from_cart_url(self) :
35 return reverse("core:remove-from-cart", kwargs={
36 "pk" : self.pk
37 })
In the item model you will see there are 3 additional functions, these
functions include:
1 class OrderItem(models.Model) :
2 user = models.ForeignKey(settings.AUTH_USER_MODEL,
3 on_delete=models.CASCADE)
4 ordered = models.BooleanField(default=False)
5 item = models.ForeignKey(Item, on_delete=models.CASCADE)
6 quantity = models.IntegerField(default=1)
7
8
9 def __str__(self):
10 return f"{self.quantity} of {self.item.item_name}"
OrderItem stores data of the product you want to order and the amount of
the product
1 class Order(models.Model) :
2 user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
3 items = models.ManyToManyField(OrderItem)
4 start_date = models.DateTimeField(auto_now_add=True)
5 ordered_date = models.DateTimeField()
6 ordered = models.BooleanField(default=False)
7
8 def __str__(self):
9 return self.user.username
The Order model will store detailed information of the orders made, but in
this part of the tutorial we will not display complete order information, we
will add another field in the next part.
open core/admin.py file witch code editor and fill with the code below :
That will register your database on the admin page. You can check it on the
admin page later
Andika7/DjangoEcommerce
Contribute to Andika7/DjangoEcommerce development by creating
an account on GitHub.
github.com
B. Managing Views
We have created a database and now it is our turn to manage and manage
our views. We will create 2 views that is HomeView and ProductView and 2
function that is add_to_cart() and remove_from_cart(). to continue open
core/views.py and continue with the following tutorial
1. Import
Import all the model classes that we have previously created into views.py
2. Add HomeView
class HomeView(ListView):
model = Item
template_name = "home.html
We use the Item model as a home model and home.html as template view.
home.html can be found at templates directory
3. Add ProductView
class ProductView(DetailView):
model = Item
template_name = "product.html"
We use the Item model as a home model and product.html as template view.
product.html can be found at templates directory
4. add_to_cart() function
This function will add your product to OrderItem database and add detail
order to Order database
5. remove_from_cart() function
This function will remove all your product from OrderItem database and
remove detail order from Order database
Andika7/DjangoEcommerce
You can't perform that action at this time. You signed in with another
tab or window. You signed out in another tab or…
github.com
C. Managing Urls
After the view is finished, then the url will be managed where the view will
be displayed in the browser. Open your core/urls.py file and fill with the
code below :
Import all view classes and functions and add the url for each view.
D. Demo Aplications
1. Run your server with the command below :
Item Database
3. Click Add to cart button in product page and you will see the product will
add to your OrderItem and Order database
Order Database
4. Click remove from cart button to remove your current order. it will delete
order item from database Order and OrderItem
Andika7/DjangoEcommerce
Contribute to Andika7/DjangoEcommerce development by creating
an account on GitHub.
github.com
71 1
Fresh Graduate of Computer Science at Universitas Syiah Kuala, Software Engineer. Check
my github on github.com/Andika7
5 min read · Jan 27, 2020 7 min read · Jan 26, 2020
1 2.4K 27
The Ultimate Markdown Guide (for How to Create your own Search
Jupyter Notebook) Engine with Python Language an…
An in-depth guide for Markdown syntax Step 4 : Implement Index and Query From
usage for Jupyter Notebook Python Language to Laravel Framework
10 min read · Nov 18, 2019 6 min read · Jan 28, 2020
2.2K 13 30 1
See all from Andika Pratama See all from Analytics Vidhya
35 17 1
Lists
3 min read · Sep 16, 2023 6 min read · Nov 12, 2023
36 6
99 13 1
Help Status About Careers Blog Privacy Terms Text to speech Teams