Here's a step-by-step guide to creating an e-commerce website with Django:
Step 1: Set up a new Django project
Open a terminal and run the following command to create a new Django project:
code Copy code
django-admin startproject ecommerce_project
This will create a new directory called ecommerce_project with the basic structure for a Django project.
Step 2: Create a new Django app
Navigate into the ecommerce_project directory and create a new Django app called store:
code Copy code
python manage.py startapp store
This will create a new directory called store with the basic structure for a Django app.
Step 3: Define the models
In the store app, define the models for the e-commerce website. For example, you might have models for products, orders, and customers.
Create a new file called models.py in the store app and add the following code:
python Copy code
from django.db import models
class 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/')
class Order(models.Model):
customer = models.ForeignKey('Customer', on_delete=models.CASCADE)
order_date = models.DateTimeField(auto_now_add=True)
total = models.DecimalField(max_digits=10, decimal_places=2)
class Customer(models.Model):
name = models.CharField(max_length=255)
email = models.EmailField(unique=True)
address = models.TextField()
Step 4: Create the database tables
Run the following command to create the database tables for the models:
code Copy code
python manage.py makemigrations
python manage.py migrate
Step 5: Create views
Create a new file called views.py in the store app and add the following code:
python Copy code
from django.shortcuts import render, redirect
from .models import Product, Order, Customer
from .forms import ProductForm, OrderForm, CustomerForm
def product_list(request):
products = Product.objects.all()
return render(request, 'store/product_list.html', {'products': products})
def product_detail(request, pk):
product = Product.objects.get(pk=pk)
return render(request, 'store/product_detail.html', {'product': product})
def order_list(request):
orders = Order.objects.all()
return render(request, 'store/order_list.html', {'orders': orders})
def order_detail(request, pk):
order = Order.objects.get(pk=pk)
return render(request, 'store/order_detail.html', {'order': order})
def customer_list(request):
customers = Customer.objects.all()
return render(request, 'store/customer_list.html', {'customers': customers})
def customer_detail(request, pk):
customer = Customer.objects.get(pk=pk)
return render(request, 'store/customer_detail.html', {'customer': customer})
Step 6: Create forms
Create a new file called forms.py in the store app and add the following code:
python Copy code
from django import forms
from .models import Product, Order, Customer
class ProductForm(forms.ModelForm):
class Meta:
model = Product
fields = ('name', 'description', 'price', 'image')
class OrderForm(forms.ModelForm):
class Meta:
model = Order
fields = ('customer', 'order_date', 'total')
class CustomerForm(forms.ModelForm):
class Meta:
model = Customer
fields = ('name', 'email', 'address')
Step 7: Create templates
Create a new directory called templates in the store app and add the following files:
product_list.html
product_detail.html
order_list.html
order_detail.html
customer_list.html
customer_detail.html
Add the following code to each template:
html Copy code
<!-- product_list.html -->
{% extends 'base.html' %}
{% block content %}
<h1>Products</h1>
<ul>
{% for product in products %}
<li>{{ product.name }} ({{ product.price }})</li>
{% endfor %}
</ul>
{% endblock %}
<!-- product_detail.html -->
{% extends 'base.html' %}
{% block content %}
<h1>{{ product.name }}</h1>
<p>{{ product.description }}</p>
<p>Price: {{ product.price }}</p>
<img src="{{ product.image.url }}" alt="Product image">
{% endblock %}
<!-- order_list.html -->
{% extends 'base.html' %}
{% block content %}
<h1>Orders</h1>
<ul>
{% for order in orders %}
<li>{{ order.customer.name }} ({{ order.order_date }})</li>
{% endfor %}
</ul>
{% endblock %}
<!-- order_detail.html -->
{% extends 'base.html' %}
{% block content %}
<h1>{{ order.customer.name }}'s Order</h1>
<p>Order date: {{ order.order_date }}</p>
<p>Total: {{ order.total }}</p>
{% endblock %}
<!-- customer_list.html -->
{% extends 'base.html' %}
{% block content %}
<h1>Customers</h1>
<ul>
{% for customer in customers %}
<li>{{ customer.name }} ({{ customer.email }})</li>
{% endfor %}
</ul>
{% endblock %}
<!-- customer_detail.html -->
{% extends 'base.html' %}
{% block content %}
<h1>{{ customer.name }}</h1>
<p>Email: {{ customer.email }}</p>
<p>Address: {{ customer.address }}</p>
{% endblock %}
Step 8: Create a base template
Create a new file called base.html in the templates directory and add the following code:
html Copy code
<!DOCTYPE html>
<html>
<head>
<title>Ecommerce Website</title>
</head>
<body>
<nav>
<ul>
<li><a href="{% url 'product_list' %}">Products</a></li>
<li><a href="{% url 'order_list' %}">Orders</a></li>
<li><a href="{% url 'customer_list' %}">Customers</a></li>
</ul>
</nav>
<div id="content">
{% block content %}{% endblock %}
</div>
</body>
</html>
Step 9: Define URLs
Create a new file called urls.py in the store app and add the following code:
python Copy code
from django.urls import path
from . import views
urlpatterns = [
path('products/', views.product_list, name='product_list'),
path('products/<pk>/', views.product_detail, name='product_detail'),
path('orders/', views.order_list, name='order_list'),
path('orders/<pk>/', views.order_detail, name='order_detail'),
path('customers/', views.customer_list, name='customer_list'),
path('customers/<pk>/', views.customer_detail, name='customer_detail'),
]
Step 10: Run the development server
Run the following command to start the development server:
code Copy code
python manage.py runserver
Open a web browser and navigate to http://localhost:8000/products/ to see the product list page.
That's it! You now have a basic e-commerce website with Django. Of course, this is just the beginning, and you'll likely want to add more features, such as user authentication, payment processing, and more. But this should give you a good starting point.