yourlabs-subscription 错误——渲染时捕获 VariableDoesNotExist
我一直在尝试使用更新后的 yourlabs 订阅示例,并且安装工作正常,除了
- 返回渲染到模板中的请求变量。
-
模板中未收到此
请求上下文
变量,因此我收到以下错误渲染时捕获 VariableDoesNotExist:在 u 中查找关键 [请求] 失败
因为这是在每个视图中返回的,所以我无法通过在特定模板中进行一些调整来解决此问题
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生这种情况是因为
request
不在模板的上下文中,并且模板正在使用一些期望它存在的模板代码。该代码(例如自定义模板标签)应该 更好地处理VariableDoesNotExist
此外,您的视图可能不应该在每个响应中显式返回
request
。让 Django 为你处理这个问题。为此,请添加 请求模板随 Django 一起提供给你的 href="https://docs.djangoproject.com/en/1.3/ref/settings/#std%3asetting-TEMPLATE_CONTEXT_PROCESSORS" rel="nofollow">
TEMPLATE_CONTEXT_PROCESSORS
:如果您是已经使用此模板上下文处理器,请确保调用
render_to_response
context_instance=RequestContext(request)
作为最后一个参数(下面的示例来自 docs):这确保了
TEMPLATE_CONTEXT_PROCESSORS
中模板上下文处理器返回的所有字典都是传递给模板。您还可以使用
render
快捷方式,它将自动使用Requestcontext
实例呈现模板。Django 1.3 中添加的另一个选项是
TemplateResponse
,它也将使用RequestContext
的实例。This is happening because
request
isn't in your template's context and the template is using some template code that expected it to be there. That code (e.g. a custom template tag) should better handleVariableDoesNotExist
In addition, your views probably shouldn't return
request
in every response explicitly. Let Django handle this for you.To do this, add the request template context processor that ships with Django to your
TEMPLATE_CONTEXT_PROCESSORS
:If you are already using this template context processor, ensure that
render_to_response
is called withcontext_instance=RequestContext(request)
as the final argument (the below example is from the docs):This ensures that all the dicts returned by the template context processors in
TEMPLATE_CONTEXT_PROCESSORS
are passed to the template.You could also use the
render
shortcut, which will automatically render the template with an instance ofRequestcontext
.Another option added in Django 1.3 is the
TemplateResponse
, which will also use an instance ofRequestContext
.