在从Django 1.3升级到1.6时,我注意到我无法再访问'registration / login.html'中的请求 .
因为我在每个页面上都有一个购物车(包括登录页面),这现在引发了一个“/ errors / login /上的AttributeError - ''str'object没有属性'session'”,因为购物车功能无法取出购物车物品通过request.session .
作为参考,这是内置的auth.login函数:
@sensitive_post_parameters()
@csrf_protect
@never_cache
def login(request, template_name='registration/login.html',
redirect_field_name=REDIRECT_FIELD_NAME,
authentication_form=AuthenticationForm,
current_app=None, extra_context=None):
"""
Displays the login form and handles the login action.
"""
redirect_to = request.REQUEST.get(redirect_field_name, '')
if request.method == "POST":
form = authentication_form(request, data=request.POST)
if form.is_valid():
# Ensure the user-originating redirection url is safe.
if not is_safe_url(url=redirect_to, host=request.get_host()):
redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL)
# Okay, security check complete. Log the user in.
auth_login(request, form.get_user())
return HttpResponseRedirect(redirect_to)
else:
form = authentication_form(request)
current_site = get_current_site(request)
context = {
'form': form,
redirect_field_name: redirect_to,
'site': current_site,
'site_name': current_site.name,
}
if extra_context is not None:
context.update(extra_context)
return TemplateResponse(request, template_name, context,
current_app=current_app)
此函数似乎通过TemplateResponse返回请求对象,但我无法在'registration / login.html'中访问它 . 如果我删除购物车,登录正常工作 .
作为一种解决方法,我可以从登录页面中删除购物车,但我想知道为什么这已经改变了(可能是一个安全漏洞?) . 似乎在调用auth.login函数和login.html页面之间的某个地方,请求就丢失了 . 有谁知道请求发生了什么?
1 回答
好的,我想我已经解决了 .
出于某种原因,settings.py中的TEMPLATE_CONTEXT_PROCESSORS为空 . 根据the docs,Django 1.6的默认TEMPLATE_CONTEXT_PROCESSORS应为:
添加此行:
到TEMPLATE_CONTEXT_PROCESSORS设置使一切按预期工作 . 现在我想知道为什么当TEMPLATE_CONTEXT_PROCESSORS为空时,网站的其余部分正在工作! (如果没有设置,我的其他页面如何能够访问请求?)