This is an open-source Django REST API project for managing an e-commerce cart. It provides functionality for user authentication, creating and managing carts, and handling cart items.
This project is developed by me and the Anthropic Claude AI.
- User authentication with email and password
- JWT token-based authentication
- View cart details (total items, total price, and item details)
- CRUD operations for products
- Create and update carts
- Add and remove items from the cart
- Clone the repository:
git clone https://github.com/code-on-sunday/django-ecommerce-cart.git
- Navigate to the project directory:
cd django-ecommerce-cart
- Create a virtual environment and activate it:
python -m venv env
source env/bin/activate # On Windows, use `env\Scripts\activate`
- Install the required dependencies:
pip install -r requirements.txt
- Set up the PostgreSQL database:
docker-compose up -d
- Apply database migrations:
python manage.py migrate
- Populate the database with sample data:
python manage.py seed_users
python manage.py seed_products
python manage.py seed_carts
This will create the following sample data:
Users:
- John Doe ([email protected])
- Jane Smith ([email protected])
- Bob Johnson ([email protected])
Products:
- Product 1 (Price: $9.99)
- Product 2 (Price: $19.99)
- Product 3 (Price: $29.99)
Carts: Each user will have a cart with 1-5 random products added to it.
- Start the development server:
python manage.py runserver
The API endpoints are available at http://localhost:8000/api/
. You can use tools like Postman or cURL to interact with the API.
POST /api/login/
: Authenticate a user and obtain a JWT token.
Example with cURL:
curl -X POST -H "Content-Type: application/json" -d '{"email": "[email protected]", "password": "password1"}' http://localhost:8000/api/login/
GET /api/cart/
: Retrieve the cart details for the authenticated user.
Example with cURL (replace <token>
with the JWT token obtained from the login endpoint):
curl -H "Authorization: Bearer <token>" http://localhost:8000/api/cart/
This project includes test cases for the User, Product, Cart, and CartItem models, as well as for the authentication and cart API views. To run the tests, execute the following command:
python manage.py test api