Skip to main content

A Django app to add product to store.

Project description

Django Shopping Cart is a Django app to store product in cart.

Detailed documentation is in the “docs” directory.

Quick start

  1. Add “cart” to your INSTALLED_APPS setting like this:

    INSTALLED_APPS = [
        ...
        'cart',
    ]
    
    Add below line to settings context_processor::
        'cart.context_processor.cart_total_amount'
    
    CART_SESSION_ID = 'cart'
  2. You can use the urls in following way:

    from django.urls import path
    from . import views
    
    urlpatterns = [
        path('cart/add/<int:id>/', views.cart_add, name='cart_add'),
        path('cart/item_clear/<int:id>/', views.item_clear, name='item_clear'),
        path('cart/item_increment/<int:id>/',
             views.item_increment, name='item_increment'),
        path('cart/item_decrement/<int:id>/',
             views.item_decrement, name='item_decrement'),
        path('cart/cart_clear/', views.cart_clear, name='cart_clear'),
        path('cart/cart-detail/',views.cart_detail,name='cart_detail'),
    ]
  1. You should have a Product model & Below field is mandatory

    name = models.CharField(max_length=255) image = models.ImageField(upload_to=’products/’) price = models.FloatField()

  1. Then views.py should like like this:

    from django.shortcuts import render, redirect
    from store.models import Product
    from django.contrib.auth.decorators import login_required
    from cart.cart import Cart
    
    @login_required(login_url="/users/login")
    def cart_add(request, id):
        cart = Cart(request)
        product = Product.objects.get(id=id)
        cart.add(product=product)
        return redirect("home")
    
    
    @login_required(login_url="/users/login")
    def item_clear(request, id):
        cart = Cart(request)
        product = Product.objects.get(id=id)
        cart.remove(product)
        return redirect("cart_detail")
    
    
    @login_required(login_url="/users/login")
    def item_increment(request, id):
        cart = Cart(request)
        product = Product.objects.get(id=id)
        cart.add(product=product)
        return redirect("cart_detail")
    
    
    @login_required(login_url="/users/login")
    def item_decrement(request, id):
        cart = Cart(request)
        product = Product.objects.get(id=id)
        cart.decrement(product=product)
        return redirect("cart_detail")
    
    
    @login_required(login_url="/users/login")
    def cart_clear(request):
        cart = Cart(request)
        cart.clear()
        return redirect("cart_detail")
    
    
    @login_required(login_url="/users/login")
    def cart_detail(request):
        return render(request, 'cart/cart_detail.html')
  2. In the template you can use the url in folowing way:

    <a href="{% url 'cart_add' product.id %}">Add To Cart</a>
    <a href="{% url 'cart_clear' %}">Clear Cart</a>
    <a href="{% url 'item_increment' value.product_id %}">Increament</a>
    <a href="{% url 'item_decrement' value.product_id %}">Decrement</a>
  3. To view the cart detail page use the below code

    {% load cart_tag %}

    Total Length :: {{request.session.cart|length}}

    Cart Detail:

    {% for key,value in request.session.cart.items %}

    {{value.name}} {{value.price}} {{value.quantity}} {{value.image}} Total {{ value.price|multiply:value.quantity }}

    {% endfor %}

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

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

Source Distribution

cartAMDI-0.1.tar.gz (6.2 kB view details)

Uploaded Source

File details

Details for the file cartAMDI-0.1.tar.gz.

File metadata

  • Download URL: cartAMDI-0.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.21.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for cartAMDI-0.1.tar.gz
Algorithm Hash digest
SHA256 d31cb54642d7f1e7a5743d7f328f7d43cbe7eaa34956e77a5e9f8e7a990667bb
MD5 2a28dbbffd1ff1b1bbf6025d36379ef4
BLAKE2b-256 c89235bc701f8d0600a308bdeba0e5c88ddcbb05af4be59e876cd29a9b372af2

See more details on using hashes here.

Supported by

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