0

Im trying to create a django cart page. im having an issue adding products/plans to a cart and im also having an issue on showing a quantity of items in the cart. Ive made a context.py to try add to the cart but im not too sure what im doing wrong. im getting an error message

ImportError at /
Module "cart.context" does not define a "cart_context" attribute/class

Heres my context.py file

from django.shortcuts import get_object_or_404
from plans.models import Plans


def cart_contents(request):
    """
    Enables the cart contents to be shown when
    rendering any page on the site.
    """

    cart = request.session.get('cart', {})

    cart_items = []
    total = 0
    product_count = 0

    for (id, quantity) in cart.items():
        product = get_object_or_404(Product, pk=id)
        total += quantity * product.price
        product_count += quantity
        cart_items.append({'id': id, 'quantity': quantity,
                          'product': product})

    return {'cart_items': cart_items, 'total': total,
            'product_count': product_count}

Heres my cart views

from django.shortcuts import render, redirect, reverse

# Create your views here.

def view_cart(request):
    """ A view that renders the cart page """

    return render(request, 'cart/cart.html')

def add_to_cart(request, item_id):
    """ Add plan to shopping cart """
    
    cart = request.session.get('cart', {})
    cart[item_id] = cart.get(item_id, 1)

    request.session['cart'] = cart
    return redirect(reverse('plans'))

Add here is my navbar cart link, where i want the number to show up

<li class="nav-item">
                        <a class="nav-link" href="{% url 'view_cart' %}"><i class="fa fa-shopping-cart"></i> Cart
                           {% if plan_count > 0 %}
                           <label class="badge badge-warning">{{ plan_count }}</label>
                           {% endif %}
                        </a>
                     </li>

Here is my settings.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),
            os.path.join(BASE_DIR, 'templates', 'allauth'),
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request', # Required by Allauth
                'django.template.context_processors.static',
                'django.contrib.auth.context_processors.auth', 
                'django.contrib.messages.context_processors.messages',
                'cart.context.cart_context',
                'ms4.context.orderCount'
            ],
        },
    },
]
amit238
  • 21
  • 4

1 Answers1

0

Please rename context.py to context_processors.py and edit like given below.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),
            os.path.join(BASE_DIR, 'templates', 'allauth'),
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request', # Required by Allauth
                'django.template.context_processors.static',
                'django.contrib.auth.context_processors.auth', 
                'django.contrib.messages.context_processors.messages',
                'cart.context_processors.cart_context',
                'ms4.context.orderCount'
            ],
        },
    },
]

please try this.

Riyas Ac
  • 1,553
  • 1
  • 9
  • 23