Create E-Commerce Website Using Django Rest Framework
Shikha Talaviya
AI/ML Engineer | Driving AI Innovation, Growth, & Strategy at Optodolce | Expert in GenAI, Virtual Influencers & Fundraising
Step 1: Install Django and Django Rest Framework
First, we need to install Django and Django Rest Framework on our system. We can follow the installation steps from the official documentation of Django and Django Rest Framework.
Step 2: Create a new Django project After installing Django and Django Rest Framework, create a new Django project using the following command:
django-admin startproject ecommerce
Step 3: Create a new Django app Create a new Django app using the following command:
python manage.py startapp api
Step 4: Create models for the e-commerce website Create the following models for the e-commerce website:
from django.db import model
class Product(models.Model):
???name = models.CharField(max_length=200)
???price = models.DecimalField(max_digits=10, decimal_places=2)
???description = models.TextField()
???image = models.ImageField(upload_to='images/')
???def __str__(self):
???????return self.name
class Category(models.Model):
???name = models.CharField(max_length=200)
???def __str__(self):
???????return self.name
class ProductCategory(models.Model):
???product = models.ForeignKey(Product, on_delete=models.CASCADE)
???category = models.ForeignKey(Category, on_delete=models.CASCADE)s
Step 5: Create serializers for the models Create serializers for the models using the following code:
from rest_framework import serializer
from .models import Product, Category, ProductCategory
class ProductSerializer(serializers.ModelSerializer):
???class Meta:
???????model = Product
???????fields = '__all__'
class CategorySerializer(serializers.ModelSerializer):
???class Meta:
???????model = Category
???????fields = '__all__'
class ProductCategorySerializer(serializers.ModelSerializer):
???class Meta:
???????model = ProductCategory
???????fields = '__all__'s
Step 6: Create views for the e-commerce website Create views for the e-commerce website using the following code:
from rest_framework import generics
from .models import Product, Category, ProductCategory
from .serializers import ProductSerializer, CategorySerializer, ProductCategorySerializer
class ProductList(generics.ListCreateAPIView):
queryset = Product.objects.all()
serializer_class = ProductSerializer
class ProductDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Product.objects.all()
serializer_class = ProductSerializer
class CategoryList(generics.ListCreateAPIView):
queryset = Category.objects.all()
serializer_class = CategorySerializer
class CategoryDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Category.objects.all()
serializer_class = CategorySerializer
class ProductCategoryList(generics.ListCreateAPIView):
queryset = ProductCategory.objects.all()
serializer_class = ProductCategorySerializer
class ProductCategoryDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = ProductCategory.objects.all()
serializer_class = ProductCategorySerialize
Step 7: Create urls for the views using the following code:
领英推荐
from django.urls import path
from .views import ProductList, ProductDetail, CategoryList, CategoryDetail, ProductCategoryList, ProductCategoryDetail
urlpatterns = [
path('products/', ProductList.as_view()),
path('products//', ProductDetail.as_view()),
path('categories/', CategoryList.as_view()),
path('categories//', CategoryDetail.as_view()),
path('productcategories/', ProductCategoryList.as_view()),
path('productcategories//', ProductCategoryDetail.as_view()),
]
Step 8: Create urls for the API using the following code:
from django.urls import path, include
from rest_framework.routers import DefaultRouter
router = DefaultRouter() router.register('products', ProductList) router.register('categories', CategoryList) router.register('productcategories', ProductCategoryList) urlpatterns = [ path('', include(router.urls)), ]
Step 9: Add authentication to the API :
To add authentication to the API, we can use Django Rest Framework's built-in authentication system. We can follow the steps from the official documentation of Django Rest Framework for adding authentication to the API.
Step 10: Run the server
To run the server, we can use the following command:
python manage.py runserver
And that's it! We have created an e-commerce website using Django Rest Framework and API.emwork and API.
Note: This is just a basic example and we can add more features to the e-commerce website as per our requirement.
Those who want to learn Python, Machine Learning, Deep Learning, AI, Data Science can also contact me.