简体   繁体   English

Context_processor未定义“”类/属性(错误)

[英]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.

相关问题 @context_processor如何影响模板变量? - How does @context_processor effect template variables? 龙卷风框架的context_processor - context_processor for tornado framework 如何在django中正确使用context_processor - how to use context_processor properly in django 从表中获取列作为数组在网页上使用 context_processor - Getting column from table as an array to using context_processor on webpage 为flask中的context_processor设置默认模板过滤器 - Set the default template filter for the context_processor in flask 如何从 Flask 的方法路由中访问 c​​ontext_processor? - How do I access a context_processor from within a method route in Flask? Flask:如何在另一个蓝图中使用一个蓝图中的 context_processor - Flask: How can i use a context_processor from one blueprint in another blueprint 模块“django.template.context_processors”没有定义“custom_proc”属性/类 - Module "django.template.context_processors" does not define a "custom_proc" attribute/class Django ImportError:模块未定义属性/类 - Django ImportError: Module does not define attribute/class ImportError:模块“ whitenoise.middleware”未定义“ WhiteNoiseMiddleWare”属性/类 - ImportError: Module “whitenoise.middleware” does not define a “WhiteNoiseMiddleWare” attribute/class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM