Making an excellent shopping site p2
In the previous article we have discussed making a shopping site of frontend using HTML, CSS, and Bootstrap. In making an excellent shopping site p2 we will be discussing building the backend using django. Django is a very powerful framework from a well known high level language called Python. Python has been a very powerful language since previous years. So without further delay let’s get started.
What is Django ?:
As I said before, Django is a framework from a very high level language python, that’s why it’s gonna be a very powerful framework, and also hard somewhere. But in this article you will be going to understand it’s functionality for making a shopping site, the installation and basics are on another article you can watch all out from here.
If your project structure is like below then you can proceed after.
<<your project folder>> |___ <<your App folder>> |___ migrations |___ __pycache__(folder) |__ static(folder) |___ __init__.py |___ admin.py |___ apps.py |___ models.py |___ urls.py |___ views.py |___ <<folder named your project>> |___ __pycache__(folder) |___ __init__.py |___ asgi.py |___ settings.py |___ urls.py |___ wsgi.py |___ templates(folder) |___ db.sqlite3 |___ manage.py
Modification on models.py file:
As you know that models.py file is used to create tables on the database, so we are going to create some important tables on the database. In a shopping site there will be definitely a users table, so that users will be able to get signed up and login. Then, there must be a products table so that the owner of the site can display products on the site.
Then there will be a comments table to allow users to leave comments on each product. There must be a review & rating table so that every user will be able to leave reviewed products. There also be a table called purchased items, there all the transactions and purchased items. There will be some more tables that will be made.
In these models, the users table will automatically be made by the software. Other tables will be made.
The below code is for the products Table:
class Products(models.Model): product_name = models.CharField(max_length=100) product_desc = models.TextField() product_image = models.ImageField(upload_to='images/') product_sample_image_1 = models.ImageField(upload_to='samples/') product_sample_image_2 = models.ImageField(upload_to='samples/') product_sample_image_3 = models.ImageField(upload_to='samples/') product_sample_image_4 = models.ImageField(upload_to='samples/') product_slug = models.SlugField() product_created = models.DateTimeField(auto_now_add=True) product_updated = models.DateTimeField(auto_now=True)
The below table is for comments Table:
class Comments(models.Model): product = models.ForeignKey(Products, on_delete=models.CASCADE) comment_body = models.TextField() commented = models.DateTimeField(auto_now_add=True)
The below model is for Review & Rating:
class ReviewAndRating(models.Model): product = models.ForeignKey(Product,on_delete=models.CASCADE) rating = models.FloatField() review_image = models.ImageField(upload_to='rev_img/') review_text = models.TextField() reviewed_on = models.DateTimeField(auto_now_add=True)
The below code is for Processed item:
class ProcessedItem(models.Model): user_processed = models.ForeignKey(User, on_delete=models.CASCADE) product_processed = models.ForeignKey(Product,on_delete=models.CASCADE) payment_method = models.CharField(max_length=100) payment_details = models.TextField() created = models.DateTimeField(auto_now_add=True)
This below file will be in the admin.py file:
from .models import Products,Comments,ReviewAndRating,ProcessedItem admin.site.register(Products) admin.site.register(Comments) admin.site.register(ReviewAndRating) admin.site.register(ProcessedItem)
After creating these models now save the file and go to the terminal and make migrations and after that migrate the fields and then run the server, now go to the browser and type 127.0.0.1:8000/admin/
You will be able to see the models that we created before. Now algorithms need to be added on the views.py file so that every model will work independently.
Before that we need to make the frontend. In frontend you can make the pages like home, products, productview, checkout, payment, success-page, error-page, tracking-page etc.
We’ll cover the views.py algorithm in another article.
I think you understand this whole procedure, if you have any doubts then you can ask me at the contact page, I will definitely answer your question.
for django related details go to: https://docs.djangoproject.com/en/3.1/
This is only the second part of making a shopping site. There are many articles coming up, so get connected with our social accounts, from the top right floating social icons.
We have covered what is django, django project structure, creating models.py file, making migrations and migrating, then making the frontend.