django自定义templatetag不在上下文中获取请求

django自定义templatetag不在上下文中获取请求

本文介绍了django自定义templatetag不在上下文中获取请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django 1.4并尝试转换添加到自定义标签中。这意味着我需要从请求中访问is_secure和site_name值。这是我在settings.py中的CONTEXT_PROCESSORS:

I am using django 1.4 and trying to convert the code described at the end of this article into a customtag. This means I need access to the is_secure and site_name values from the request. Here is my CONTEXT_PROCESSORS in settings.py:

CONTEXT_PROCESSORS = (
    'django.core.context_processors.request',
    'django.contrib.auth.context_processors.auth',
)

这是我的模板标记代码:

Here is my template tag code:

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

在我的查看代码中,我正在使用新的渲染快捷方式,例如:

In my view code I am using the new render shortcut like so:

return render(request, 'myapp/mytemplate.html', {'foo':bar})

我在模板中这样称呼它:

And I am calling it like this in the template:

{% full_static_url "images/logo.gif" %}

问题是,当到达 request = context ['request'] 行时,它会引发KeyError,因为'request

The problem is, when it gets to the line request = context['request'] it throws a KeyError because 'request' is not in context.

我在这里做什么错了?

完整的回溯是:

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'


推荐答案

解决此问题的正确方法是添加 TEMPLATE_CONTEXT_PROCESSORS + =( django.core.context_processors.request,)在您的settings.py文件上。

The right way to fix this issue is to add TEMPLATE_CONTEXT_PROCESSORS += ("django.core.context_processors.request",) on your settings.py file.

请确保先导入 TEMPLATE_CONTEXT_PROCESSORS ,然后导入从django.conf.global_settings导入TEMPLATE_CONTEXT_PROCESSORS 否则将不起作用。

Ensure to import TEMPLATE_CONTEXT_PROCESSORS first with from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS or it will not work.

这篇关于django自定义templatetag不在上下文中获取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 04:43