问题
I have this in my setting.py:
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
'django.core.context_processors.static',
'django.contrib.auth.context_processors.auth',
)
Whenever I try to access the request in a template (for example {{ request.user.get_profile.custom_username }}) I get no result. I think the request is not added to the template, because if i force the request in to the Context (in the view), I can access it:
ctx = {}
#ctx['request'] = request
return render_to_response('index.html', ctx)
Any help? Thanks
回答1:
Use the render shortcut instead, easier to remember:
return render(request, 'index.html', ctx)
回答2:
RequestContext needs to be passed to the template, in django 1.3. you can use render to automatically include this or if you need to use render_to_response try:
return render_to_response('index.html',
ctx,
context_instance=RequestContext(request))
来源:https://stackoverflow.com/questions/9259298/django-core-context-processors-request-not-working