how to activate DJANGO.CORE.CONTEXT_PROCESSORS.REQUEST

不羁的心 提交于 2019-12-05 00:30:46

问题


I read this

"DJANGO.CORE.CONTEXT_PROCESSORS.REQUEST If TEMPLATE_CONTEXT_PROCESSORS contains this processor, every RequestContext will contain a variable request, which is the current HttpRequest. Note that this processor is not enabled by default; you'll have to activate it. " from this page

http://docs.djangoproject.com/en/dev/ref/templates/api/

But it seems there is no information how to activate this processor.

Here is my original question

Access request in django custom template tags

After i followed the answer

i still got errors

TemplateSyntaxError at / Caught an exception while rendering: 'request' Original Traceback (most recent call last): 
File "C:\Python25\lib\site-packages\django\template\debug.py", line 71, in render_node result = node.render(context) 
File "C:\Python25\lib\site-packages\django\template__init__.py", line 936, in render dict = func(*args)
 File "c:\...\myapp_extras.py", line 7, in login request = context['request'] 
File "C:\Python25\lib\site-packages\django\template\context.py", line 44, in getitem raise KeyError(key) KeyError: 'request'

the code causing problem is

request = context['request'] in

from django import template

register = template.Library()


@register.inclusion_tag('userinfo.html',takes_context = True)
def userinfo(context):
 request = context['request']
 address = request.session['address']
 return {'address':address}

回答1:


I answered this here: How can I pass data to any template from any view in Django?

Also see the comments on my answer... you might want that bit of info too.




回答2:


in settings.py

from django.conf import global_settings

TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
    'django.core.context_processors.request',
)


来源:https://stackoverflow.com/questions/2202328/how-to-activate-django-core-context-processors-request

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