A simple and flexible shopping cart for Django projects .
django-easycart-ng is a lightweight, reusable shopping cart app for Django. It provides essential cart functionality with a clean API, making it easy to integrate into any Django project.
- Simple Cart Management: Add, remove, and update items with ease.
- Generic Product Support: Works with any product model using a generic
product_idfield. - Wishlist: Allow users to save products for later.
- Session Support: (Coming soon) Support for anonymous users via sessions.
- Admin Integration: Full admin interface for managing carts and wishlists.
- Template Tags: Easy access to cart data in templates.
- Lightweight: Minimal dependencies (only Django).
- Extensible: Easy to customize and extend.
- Install the package:
pip install django-easycart-ng- Add
cartto yourINSTALLED_APPS:
INSTALLED_APPS = [
# ...
"cart.apps.CartConfig",
# ...
]- Run migrations:
python manage.py migrate- Add the context processor (optional but recommended):
TEMPLATES = [
{
# ...
"OPTIONS": {
"context_processors": [
# ...
"cart.context_processors.cart_total",
],
},
},
]Add an item to the cart:
from cart.models import Cart
# Get or create the user's cart
cart, _ = Cart.objects.get_or_create(user=request.user)
# Add an item
cart.add_item(
product_id=1,
quantity=2,
price=99.99,
size="Large", # Extra data
)Get cart total:
total_price = cart.get_total_price()
total_items = cart.get_total_items()Use template tags:
{% load cart_tags %}
<p>Total items: {% get_cart_total_items %}</p>
<p>Total price: ${% get_cart_total_price %}</p>
{% if request.user.is_authenticated %}
<a href="{% url 'cart_detail' %}">View Cart</a>
{% endif %}| Method | Description |
|---|---|
add_item(product_id, quantity=1, price=None, **extra_data) |
Add an item to the cart. |
remove_item(item_id) |
Remove an item from the cart. |
update_quantity(item_id, quantity) |
Update the quantity of an item. |
get_total_price() |
Return total price of all items. |
get_total_items() |
Return total number of items. |
clear() |
Remove all items from the cart. |
| Method | Description |
|---|---|
add_product(product_id) |
Add a product to the wishlist. |
remove_product(product_id) |
Remove a product from the wishlist. |
has_product(product_id) |
Check if a product is in the wishlist. |
get_product_count() |
Return the number of products in the wishlist. |
To contribute to this package, follow these steps:
- Clone the repository:
git clone https://github.com/reza-khalili-dev/django-easycart-ng.git
cd django-easycart-ng- Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install development dependencies:
pip install -e .[dev]- Run tests:
pytestThis project is licensed under the MIT License. See the LICENSE file for details.
If you find this package useful, please give it a ⭐ on GitHub!
- Author: Reza Khalili
- Email: [email protected]
- Linkedin www.linkedin.com/in/reza-khalili-developer
- GitHub: https://github.com/reza-khalili-dev