Skip to main content

Simple django cart manager for your django projects.

Project description

dj-shop-cart

A simple and flexible cart manager for your django projects.

pypi python django MIT License black

✨📚✨ Read the full documentation

Features

  • Add, remove, decrement and clear items from cart
  • Authenticated users cart can be saved to database
  • Write custom methods to easily hook into the items add / remove flow
  • Custom get_price method to ensure that the cart always have an up-to-date products price
  • Each item in the cart hold a reference to the associated product
  • Metadata data can be attached to cart items
  • Supports specification of product variation details
  • Available context processor for easy access to the user cart in all your django templates
  • Swappable backend storage, with session and database provided by default

Installation

Install dj-shop-cart with pip or poetry.

  pip install dj-shop-cart

Quickstart

# settings.py

TEMPLATES = [
    {
        "OPTIONS": {
            "context_processors": [
                ...,
                "dj_shop_cart.context_processors.cart", # If you want access to the cart instance in all templates
            ],
        },
    }
]

# models.py

from django.db import models
from dj_shop_cart.cart import CartItem
from dj_shop_cart.protocols import Numeric

class Product(models.Model):
    ...

    def get_price(self, item:CartItem) -> Numeric:
        """The only requirements of the dj_shop_cart package apart from the fact that the products you add
        to the cart must be instances of django based models. You can use a different name for this method
        but be sure to update the corresponding setting (see Configuration). Even if you change the name the
        function signature should match this one.
        """


# views.py

from dj_shop_cart.cart import Cart
from django.http import HttpRequest
from django.views.decorators.http import require_POST
from django.shortcuts import get_object_or_404

from .models import Product


@require_POST
def add_product(request: HttpRequest, product_id:int):
    product = get_object_or_404(Product.objects.all(), pk=product_id)
    quantity = int(request.POST.get("quantity"))
    cart = Cart.new(request)
    cart.add(product, quantity=quantity)
    ...


@require_POST
def remove_product(request: HttpRequest):
    item_id = request.POST.get("item_id")
    quantity = int(request.POST.get("quantity"))
    cart = Cart.new(request)
    cart.remove(item_id=item_id, quantity=quantity)
    ...


@require_POST
def empty_cart(request: HttpRequest):
    Cart.new(request).empty()
    ...

Used By

This project is used by the following companies:

Development

Poetry is required (not really, you can set up the environment however you want and install the requirements manually) to set up a virtualenv, install it then run the following:

poetry install
pre-commit install --install-hooks

Tests can then be run quickly in that environment:

pytest

Feedback

If you have any feedback, please reach out to me at tobidegnon@proton.me.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dj_shop_cart-8.0.1.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dj_shop_cart-8.0.1-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file dj_shop_cart-8.0.1.tar.gz.

File metadata

  • Download URL: dj_shop_cart-8.0.1.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Bluefin","version":"43","id":"Deinonychus","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for dj_shop_cart-8.0.1.tar.gz
Algorithm Hash digest
SHA256 8906f08ff977e745201ce14b185c38168ab41c2b7178cdf35d44bb6facb7aae7
MD5 e7f6301be14706456e83182aa79e921f
BLAKE2b-256 2f434274061e70a0a7c495ef867ba8e6f9432ec43acce4a900cf5f074facf07c

See more details on using hashes here.

File details

Details for the file dj_shop_cart-8.0.1-py3-none-any.whl.

File metadata

  • Download URL: dj_shop_cart-8.0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Bluefin","version":"43","id":"Deinonychus","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for dj_shop_cart-8.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7b70021a06dd8042c5e58e012d9d2ab5360b5ae7355f4682821f0022c69ed8d2
MD5 119de5ca39b276c3826277430c5770c6
BLAKE2b-256 116b43791b383e59ce2459dcf59bf6c4345e54a08ac2aa32849d7c99e910f94f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page