[英]Context_processor does not define a “ ” class/attribute(error)
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. 我正在学习Antonio's Melle Book的Django课程,并且需要一个context_processor才能在整个Web应用程序中使用购物车实例。 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 我尝试将购物车放入try catch语句中,我已经阅读了文档,但没有为我解决问题
context_processors.py context_processors.py
from .cart import Cart
def cart(request):
return {'cart': Cart(request)}
settings.py 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): cart.py类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',]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.