django - 如何激活 DJANGO.CORE.CONTEXT_PROCESSORS.REQUEST

标签 django django-templates django-custom-tags

我读了这个

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.



来自 this page

但似乎没有信息如何激活这个处理器。

这是我原来的问题

Access request in django custom template tags

在我按照答案之后

我仍然有错误
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'

导致问题的代码是

request = context['request']
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}

最佳答案

我在这里回答:How can I pass data to any template from any view in Django?

另请参阅对我的回答的评论……您可能也需要这些信息。

关于django - 如何激活 DJANGO.CORE.CONTEXT_PROCESSORS.REQUEST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2202328/

相关文章:

python - 使用内置 View 登录

python - 编写将分钟整数转换为格式为 "X hour(s) Y minute(s)"的字符串的函数的最佳方法是什么?

Django : 'tag' is not a registered tag library error

python - Django,模板上下文处理器

python - Django 不匹配 url 中的 unicode

html - 在 Django if/else 语句中用 CSS 替换图像

python - djangocms-installer 不询问设置问题

html - 如何将一个模板插入另一个模板?

python - 自定义模板标签-返回对象

python - django 以每周每月每天的方式对查询元素进行排序