django custom templatetag未在上下文中获取请求
我正在使用django 1.4,并试图将本文末尾描述的代码转换为customtag。这意味着我需要从请求中访问is_secure和site_name值。以下是settings.py中的我的上下文处理器:django custom templatetag未在上下文中获取请求,django,django-templates,Django,Django Templates,我正在使用django 1.4,并试图将本文末尾描述的代码转换为customtag。这意味着我需要从请求中访问is_secure和site_name值。以下是settings.py中的我的上下文处理器: CONTEXT_PROCESSORS = ( 'django.core.context_processors.request', 'django.contrib.auth.context_processors.auth', ) 以下是我的模板标记代码: from django
CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
'django.contrib.auth.context_processors.auth',
)
以下是我的模板标记代码:
from django import template
register = template.Library()
@register.simple_tag(takes_context=True)
def full_static_url(context, url):
request = context['request']
scheme = 'http'
if request.is_secure:
scheme += 's'
return scheme + '://' + request.site_name + context['STATIC_URL'] + url
在我的视图代码中,我正在使用新的渲染快捷方式,如下所示:
return render(request, 'myapp/mytemplate.html', {'foo':bar})
我在模板中这样称呼它:
{% full_static_url "images/logo.gif" %}
问题是,当它到达行request=context['request']时,它抛出一个键错误,因为'request'不在上下文中
我做错了什么
完全回溯是:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "C:\Projects\blah\blah\myapp\views\myview.py" in manifestcosts
44. return render(request, 'myapp/mytemplate.html', {'foo':bar})
File "C:\Python27\lib\site-packages\django\shortcuts\__init__.py" in render
44. return HttpResponse(loader.render_to_string(*args, **kwargs),
File "C:\Python27\lib\site-packages\django\template\loader.py" in render_to_string
176. return t.render(context_instance)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
140. return self._render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in _render
134. return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
823. bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
74. return node.render(context)
File "C:\Python27\lib\site-packages\django\template\defaulttags.py" in render
185. nodelist.append(node.render(context))
File "C:\Python27\lib\site-packages\django\template\base.py" in render
1107. return func(*resolved_args, **resolved_kwargs)
File "C:\Projects\blah\blah\myapp\templatetags\mytags.py" in full_static_url
25. request = context['request'] #TODO this fails with an KeyError, don't know why
File "C:\Python27\lib\site-packages\django\template\context.py" in __getitem__
54. raise KeyError(key)
Exception Type: KeyError at /myapp/myurl/110505081136179000/
Exception Value: 'request'
问题很可能是使用常规上下文呈现模板,如下所示:
return render_to_response("myapp/template.html", {"some_var": a_value})
请记住,上下文处理器仅应用于RequestContext
实例。这意味着您必须在render\u to\u响应中显式创建RequestContext
:
return render_to_response("myapp/template.html", {"some_var": a_value},
context_instance=RequestContext(request))
或者更好地使用新的渲染
快捷方式:
return render(request, "myapp/template.html", {"some_var": a_value})
我通过改变来解决它
return render(request, 'myapp/mytemplate.html', {'foo':bar})
到
我希望这能帮助其他人,我浪费了大约8个小时:p我在render()中的template.Node对象中发生了这种情况。事实证明,有时上下文中包含“请求”,而有时则没有
像其他人建议的那样,RequestContext(request)
是关键。我的猜测是,有时调用上下文时,请求没有像这样初始化
我把我的功能从
def render(self, context):
request = context['request'] # Failing here
到
一切都很顺利
如果上下文对象没有正确初始化,这将强制请求对象。出于某种原因,我不得不添加两次['request'],但似乎效果不错
编辑:我说得太快了,似乎无法修复空白上下文。相反,您可以尝试解决方法:
request = context.get('request')
if request is None:
return ''
我的页面似乎仍然可以正常工作,所以我不确定这些不好的上下文是从哪里来的。解决此问题的正确方法是在settings.py文件中添加TEMPLATE\u CONTEXT\u PROCESSORS+=(“django.core.CONTEXT\u PROCESSORS.request”)
确保先从django.conf.global\u settings导入TEMPLATE\u CONTEXT\u处理器
,否则它将无法工作。实际上,我正在使用渲染快捷方式。我应该在我的描述中提到这一点。我现在将添加它。以防万一,我尝试了render_to_response方法(使用RequestContext)同样的问题。那么,如果代码看起来正确,您能粘贴您得到的确切回溯吗?将堆栈跟踪添加到上述问题中好的,我突然想到两件事:1)尝试在视图中创建一个RequestContext
实例,并记录其dicts
属性(打印或其他),查看它是否包含request
项。2) 尝试从模板标记内部记录context.dicts
,以检查是否获得了正确的上下文实例。尝试此操作@我试过了,但正如我所怀疑的,是同一个问题。当它试图将请求从上下文映射中取出时出现KeyError。正如正常检查一样,您的设置中是否有真正的上下文处理器,或者更好的变体模板上下文处理器。py?抱歉,我不得不跳到其他任务上,现在才回到这个问题@我真的在使用上下文处理器,我不知道模板上下文处理器。我会尝试一下,让你知道。你有没有找到解决办法?我现在也有同样的问题。我的代码看起来一切正常,但请求对象没有被传递。第二个代码是错误的,render已经这样做了。您可能缺少TEMPLATE\u context\u processors
中的django.core.context\u processors.request
。这是可行的,但我认为这不是最好的解决方案。我认为锂元素添加到模板上下文处理器的解决方案是一个更好的解决方案。这就是我在estecb对Pheonix的回答发表评论后所做的。你是第一个真正把这写下来作为答案的人。谢谢在Django的最新版本中,模板上下文处理器已被弃用,并已被模板['OPTIONS']['CONTEXT\u PROCESSORS']取代。你知道在那种情况下应该进口什么吗?
def render(self, context):
request = RequestContext(context)['request']['request']
request = context.get('request')
if request is None:
return ''