Cart option for eCommerce - lhmisho/django-eCommerce GitHub Wiki

Creating Apps for cart

django-admin startapp carts
    'carts'

Register The app on settings file

settings.py

INSTALLEDAPPS = [
    'carts',
]

Playing with model for cart

models.py

from django.db import models
from django.conf import settings
from products.models import Product
# Create your models here.

User = settings.AUTH_USER_MODEL

class Cart(models.Model):
    user        = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE)
    products    = models.ManyToManyField(Product, blank=True)
    total       = models.DecimalField(default=0.0, decimal_places=2, max_digits=100)
    updated     = models.DateTimeField(auto_now=True)
    timestamp   = models.DateTimeField(auto_now_add=True)


    def __str__(self):
        return str(self.id)

Managing views

views.py

from django.shortcuts import render

# Create your views here.
def cart_home(request):
    cart_id = request.session.get("cart_id", None)
    if cart_id is None:
        print("create new cart")
        request.session['cart_id'] = 12
    else:
        print("Cart id exists")

    return render(request, 'carts/cart_home.html', {})

Managing URLS

urls.py from django.urls import path,re_path from .views import cart_home app_name = 'carts'

urlpatterns = [

    path('', cart_home,name='cart'),

]

Managing Template

carts/cart_home.html

{% extends 'base.html'%}

{% block content%}
    <h2>Cart</h2>
{%endblock%}
⚠️ **GitHub.com Fallback** ⚠️