yourlabs-subscription 错误——渲染时捕获 VariableDoesNotExist

发布于 2024-12-23 00:59:46 字数 257 浏览 2 评论 0 原文

我一直在尝试使用更新后的 yourlabs 订阅示例,并且安装工作正常,除了

  • 返回渲染到模板中的请求变量。

  • 模板中未收到此请求上下文变量,因此我收到以下错误

    渲染时捕获 VariableDoesNotExist:在 u 中查找关键 [请求] 失败

因为这是在每个视图中返回的,所以我无法通过在特定模板中进行一些调整来解决此问题

I have been trying to use the updated yourlabs subscription example, and the installation has worked fine except that

  • Each of the views in the subscription app returns the request variable in the render to template.

  • this request context variable is not received in the template and as a result I am getting the following error

    Caught VariableDoesNotExist while rendering: Failed lookup for key [request] in u

Since this is being returned in every view I can't solve this problem by making some tweaks in a particular template

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二手情话 2024-12-30 00:59:46

发生这种情况是因为 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

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

如果您是已经使用此模板上下文处理器,请确保调用 render_to_response context_instance=RequestContext(request) 作为最后一个参数(下面的示例来自 docs):

def some_view(request):
# ...
return render_to_response('my_template.html',
                          my_data_dictionary,
                          context_instance=RequestContext(request))

这确保了 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 handle VariableDoesNotExist

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:

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

If you are already using this template context processor, ensure that render_to_response is called with context_instance=RequestContext(request) as the final argument (the below example is from the docs):

def some_view(request):
# ...
return render_to_response('my_template.html',
                          my_data_dictionary,
                          context_instance=RequestContext(request))

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 rendershortcut, which will automatically render the template with an instance of Requestcontext.

Another option added in Django 1.3 is the TemplateResponse, which will also use an instance of RequestContext.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文