django context_processors,请求的“函数”对象没有属性“路径”

时间:2018-09-05 18:15:50

标签: django request

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

所以,我开始了几个Django项目,并最终遇到了这个问题:

AttributeError: 'function' object has no attribute 'path'

在以下上下文处理器上发生的情况:

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无法根据请求找到路径对象。这是我在settings.py中的上下文处理器:

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',
            ],
        },
    },
]

有任何提示吗?

1 个答案:

答案 0 :(得分:1)

嗯,对于所有对此问题感兴趣的人来说,问题在于'render'方法。从技术上讲,基于函数的视图必须返回HttpResponse对象,而render不会这样做。

我将最后一行代码更改为

return render_to_response(promotions, request)

它就像一种魅力。