I am following a Django course from Antonio's Melle Book, and I need a context_processor to use a cart instance throught the webapp. I constantly get the error, that the context processor does not define a "cart" object attribute. Note: I am using cached sessions, if it matters
I tried putting the cart in a try catch statement, I have read the docs, I doesn't sort things out for me
context_processors.py
from .cart import Cart
def cart(request):
return {'cart': Cart(request)}
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
(...)
'cart.context_processors.cart,']}
cart.py class Cart(object):
def __init__(self, request):
self.session = request.session
cart = self.session.get(settings.CART_SESSION_ID)
if not cart:
cart = self.session[settings.CART_SESSION_ID] = {}
self.cart = cart
You haven't shown the actual error message. But the problem is probably because you've put the comma inside the quotes instead of outside. Change it to:
'cart.context_processors.cart',]}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.