Building a Simple E-commerce Store with Django and Bootstrap
Creating an e-commerce store from scratch using Django and Bootstrap can be a great way to learn full-stack development. This article will guide you through setting up a simple online store with product listings, a shopping cart, and a checkout process.
Prerequisites
Before we start, ensure you have the following installed:
Python (>=3.8)
Django (>=4.0)
Bootstrap (via CDN or static files)
SQLite (default Django database)
Virtual environment (optional but recommended)
Step 1: Setting Up the Django Project
Create a Virtual Environment
python -m venv venvsource venv/bin/activate # On Windows use `venv\Scripts\activate`
Install Django
pip install djangoCreate a New Django Project
django-admin startproject ecommercecd ecommerce
Start the Development Server
python manage.py runserverNow, visit http://127.0.0.1:8000/ to verify Django is working.
Step 2: Create the Store App
python manage.py startapp storeRegister the App in settings.py
INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','store',]
Step 3: Define the Product Model
In store/models.py:
from django.db import modelsclass Product(models.Model):name = models.CharField(max_length=255)description = models.TextField()price = models.DecimalField(max_digits=10, decimal_places=2)image = models.ImageField(upload_to='products/')def __str__(self):return self.name
Run migrations:
python manage.py makemigrationspython manage.py migrate
Step 4: Create the Product Views
In store/views.py:
from django.shortcuts import renderfrom .models import Productdef product_list(request):products = Product.objects.all()return render(request, 'store/product_list.html', {'products': products})
Step 5: Configure URLs
In ecommerce/urls.py:
from django.contrib import adminfrom django.urls import path, includeurlpatterns = [path('admin/', admin.site.urls),path('', include('store.urls')),]
Create store/urls.py:
from django.urls import pathfrom .views import product_listurlpatterns = [path('', product_list, name='product_list'),]
Step 6: Create Product List Template
In store/templates/store/product_list.html:
{% load static %}<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Product List</title><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"></head><body><div class="container mt-5"><h1 class="text-center">Products</h1><div class="row">{% for product in products %}<div class="col-md-4"><div class="card mb-4"><img src="{{ product.image.url }}" class="card-img-top" alt="{{ product.name }}"><div class="card-body"><h5 class="card-title">{{ product.name }}</h5><p class="card-text">{{ product.description }}</p><p class="card-text">${{ product.price }}</p></div></div></div>{% endfor %}</div></div></body></html>
Step 7: Add Shopping Cart Functionality
Create a Cart model in store/models.py:
class Cart(models.Model):product = models.ForeignKey(Product, on_delete=models.CASCADE)quantity = models.PositiveIntegerField(default=1)
Create a cart_add view:
def cart_add(request, product_id):product = Product.objects.get(id=product_id)cart_item, created = Cart.objects.get_or_create(product=product)cart_item.quantity += 1cart_item.save()return redirect('product_list')
Update store/urls.py:
from .views import cart_addurlpatterns += [path('cart/add/<int:product_id>/', cart_add, name='cart_add'),]
Step 8: Create Checkout Process
Define a checkout view in store/views.py:
def checkout(request):cart_items = Cart.objects.all()total = sum(item.product.price * item.quantity for item in cart_items)return render(request, 'store/checkout.html', {'cart_items': cart_items, 'total': total})
Create store/templates/store/checkout.html:
{% for item in cart_items %}<p>{{ item.product.name }} - {{ item.quantity }} x ${{ item.product.price }}</p>{% endfor %}<p>Total: ${{ total }}</p>
Update store/urls.py:
urlpatterns += [path('checkout/', checkout, name='checkout'),]
Step 9: Deploy the Store
Use Django's built-in server for local testing:
python manage.py runserverFor production, consider deploying with platforms like Heroku, DigitalOcean, or AWS.
Conclusion
This article provided a step-by-step guide to building a simple e-commerce store using Django and Bootstrap. You can extend this by adding user authentication, payment integration, and order history. Happy coding!
.jpg)
Comments
Post a Comment