首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >django context_processors,请求'function‘对象没有属性'path’

django context_processors,请求'function‘对象没有属性'path’
EN

Stack Overflow用户
提问于 2018-09-05 18:15:49
回答 1查看 431关注 0票数 0

这里第一个问题是堆栈溢出。

因此,我开始了几个Django项目,最后得到了这个问题:

代码语言:javascript
复制
AttributeError: 'function' object has no attribute 'path'

它发生在以下上下文处理器上:

代码语言:javascript
复制
def get_request_promotions(request):

   promotions = PagePromotion._default_manager.select_related() \
       .prefetch_related('content_object') \
       .filter(page_url=request.path) \
       .order_by('display_order')

   if 'q' in request.GET:
       keyword_promotions \
        = KeywordPromotion._default_manager.select_related()\
        .filter(keyword=request.GET['q'])
   if keyword_promotions.exists():
       promotions = list(chain(promotions, keyword_promotions))
   return render(promotions, request)

也就是说,Django无法根据请求找到path对象。这是我在settings.py中的上下文处理器

代码语言:javascript
复制
TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates'), ],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
            'django.template.context_processors.request',  # For EL-pagination
            'common.core.context_processors.metadata',
            'portfolio.search.context_processors.search_form',
            'store.promotions.context_processors.promotions',
            'store.checkout.context_processors.checkout',
            'common.accounts.notifications.context_processors.notifications',
            ],
        },
    },
]

有什么暗示吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-19 21:44:37

嗯,对所有可能对这个问题感兴趣的人来说,问题在于‘呈现’方法。从技术上讲,基于函数的视图必须返回一个HttpResponse对象,而呈现只是不这样做。

我已将最后一行代码更改为

代码语言:javascript
复制
return render_to_response(promotions, request)

它就像一种魅力。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52191223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档