django context_processors,请求“函数"对象没有属性“路径" [英] django context_processors, request 'function' object has no attribute 'path'
问题描述
这里的堆栈溢出问了第一个问题.
First question asked in stack overflow here.
所以,我开始了几个Django项目,并最终遇到了这个问题:
So, I am starting a few Django projects and ended up with this issue:
AttributeError: 'function' object has no attribute 'path'
在以下上下文处理器上发生的情况:
Which happens on the following context processor:
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
:
That is, Django isn't able to find the path object on request. This is my context processors in 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',
],
},
},
]
有任何提示吗?
推荐答案
嗯,对于所有对此问题感兴趣的人,问题都出在'render'方法上.从技术上讲,基于函数的视图必须返回HttpResponse对象,而render不会这样做.
Hmm, to all who might be interested in this question, the problem was with the 'render' method. Technically, a function based view must return a HttpResponse object, and render just does not do that.
我将最后一行代码更改为
I've changed the last code line to
return render_to_response(promotions, request)
它就像一种魅力.
这篇关于django context_processors,请求“函数"对象没有属性“路径"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!