django.core.context_processors.request Not working

跟風遠走 提交于 2019-12-11 11:09:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!