A full-featured e-commerce application built with Django,Django REST framework,Django-template..., designed to deliver a seamless shopping experience. The project includes REST API endpoints,This project includes features like custom user authentication, profile management, dynamic cart updates with AJAX, Stripe integration for secure payment processing, coupon functionality, and order tracking. It leverages Django templates to create an intuitive frontend experience and follows a structured backend architecture for scalability.
- Project Structure
- Features
- Setup and Installation
- Usage
- Endpoints
- Models and Relationships
- Custom Authentication Backend
- Signals
- Forms
- Utilities
- Templates and AJAX
- Stripe Integration
.
├── accounts/ # User account management
│ ├── models.py # User Profile, Contact Numbers, Addresses
│ ├── views.py # Signup, Activation, Dashboard views
│ ├── urls.py # Account endpoints
│ ├── forms.py # Signup and Activation forms
├── orders/ # Order and cart management
│ ├── models.py # Order, OrderDetail, Cart, CartDetail, Coupon
│ ├── views.py # Order views
│ ├── urls.py # Order-related endpoints
│ ├── api.py # REST API endpoints for orders, cart, and coupons
├── utils/ # Utility scripts and functions
│ ├── generate_code.py # Code generator for coupons and profiles
│ ├── backends.py # Custom backend for email/username login
├── products/ # Placeholder for product and brand models
├── settings.py # Django project settings with required libraries
└──...
-
User Authentication and Profile Management
- Registration & Activation: Users sign up with a custom
SignupForm
, and a profile is created automatically using Django signals. A verification code is emailed for activation. - Profile Management: Users can manage profile details, including multiple contact numbers and addresses.
- Registration & Activation: Users sign up with a custom
-
Dynamic Cart Management with AJAX
- Cart Functionality: Users can add products to the cart and dynamically update quantities. The cart total updates automatically with AJAX, creating a smoother shopping experience.
- Coupon Support: Users can apply discount codes to their cart, and the total recalculates instantly.
-
Order Processing
- Order Tracking: Each order has a status (Received, Processed, Shipped, Delivered) and delivery time calculated automatically.
- Order History: Users can view their past orders with details on items, quantity, price, and order status.
-
Stripe Integration for Payments
- Secure Payment Processing: Stripe is integrated for handling payments. After checkout, users are redirected to Stripe for payment and back to the app for success/failure updates.
-
API Endpoints for Cart, Orders, and Coupons
- Provides RESTful API endpoints for managing cart items, creating orders, viewing orders, and applying coupons.
-
Clone the repository:
git clone <repository_url> cd <repository_folder>
-
Create a virtual environment:
python3 -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate`
-
Install dependencies:
pip install -r requirements.txt
-
Apply migrations:
python manage.py migrate
-
Run the server:
python manage.py runserver
-
Create a superuser for accessing Django’s admin interface:
python manage.py createsuperuser
-
Access the admin dashboard:
- Go to
http://127.0.0.1:8000/admin
and log in with the superuser credentials.
- Go to
-
User Signup and Profile Activation:
- Users sign up through
/accounts/signup
, and an activation email is sent to the provided email address. - Users can activate their account with the code at
/accounts/<username>/activate
.
- Users sign up through
Endpoint | Description |
---|---|
/accounts/signup |
User registration and profile creation. |
/accounts/<username>/activate |
Activate user account with a code. |
/accounts/dashboard |
Dashboard view showing product, brand, and review counts. |
/orders |
List of all orders for the user. |
/checkout |
Checkout page. |
/add-to-cart |
Add items to the shopping cart. |
/checkout/payment-process |
Process payment. |
/checkout/payment/success |
Payment success page. |
/checkout/payment/failed |
Payment failure page. |
Endpoint | Method | Description |
---|---|---|
/api/<username>/orders |
GET | List all orders for a user. |
/api/<username>/orders/<pk> |
GET | Retrieve details of a specific order. |
/api/<username>/apply-coupon |
POST | Apply a coupon to an order. |
/api/<username>/cart |
POST, PUT, DELETE | Add, update, or remove items from the cart. |
/api/<username>/order/create |
POST | Create a new order. |
-
Profile
- Linked to
User
via a one-to-one relationship. - Includes fields like
image
andcode
(activation code).
- Linked to
-
ContactNumber
- Stores multiple contact numbers for a user with types (Primary, Secondary).
-
Address
- Stores multiple address entries for a user with types (Home, Office, Business, etc.).
-
Order
- Stores order details including status, order time, delivery time, and total amounts.
-
OrderDetail
- Stores individual line items for each order, linked to a
Product
.
- Stores individual line items for each order, linked to a
-
Cart
- Holds cart information with total cost, coupon, and a status indicator.
-
CartDetail
- Stores details of each cart item.
-
Coupon
- Stores coupon codes with start and end dates, quantity, and discount amount.
The UsernameOrEmailLogin
backend allows users to log in using either their email or username. It tries to find a user by email first and falls back to username if email is not found.
Code Example:
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import User
class UsernameOrEmailLogin(ModelBackend):
def authenticate(self, request, username=None, password=None, **kwargs):
try:
user = User.objects.get(email=username)
except User.DoesNotExist:
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
return None
if user.check_password(password):
return user
return None
Django signals are used to automatically create a Profile
instance whenever a new User
is created. This ensures that each user has an associated profile right after registration.
Code Example:
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import User
from .models import Profile
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
-
SignupForm
- A form for registering a new user with username, email, and password fields.
-
ActivateUserForm
- A form for entering an activation code to activate a user account.
Code Example:
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class SignupForm(UserCreationForm):
class Meta:
model = User
fields = ['username', 'email', 'password1', 'password2']
class ActivateUserForm(forms.Form):
code = forms.CharField(max_length=10)
-
generate_code.py
- Contains a function to generate unique codes for user profiles, orders, and coupons.
-
backends.py
- Holds custom authentication backends such as
UsernameOrEmailLogin
.
- Holds custom authentication backends such as
- AJAX Cart Updates: AJAX is used to refresh the cart total dynamically when items are added or removed, without reloading the page.
- Templates: The application uses Django templates to render views. Key templates include:
cart.html
for displaying cart detailscheckout.html
for processing ordersdashboard.html
for user account management
- Checkout Process: Users are redirected to Stripe for secure payment.
- Payment Confirmation: Upon successful payment, users are redirected back to the application to view their order confirmation.
- Error Handling: Failed payments are also handled with appropriate notifications.