Django在DEBUG模式下不会创建{{ debug }}变量

3
根据 文档,如果满足所有要求,模板中应该可以使用名为“debug”和“sql_queries”的变量。
我已经设置了以下内容(并使用调试工具栏检查了它们的值):
- DEBUG = True - TEMPLATE_DEBUG = True - TEMPLATE_CONTEXT_PROCESSORS保持默认值(包含'django.core.context_processors.debug') - INTERNAL_IPS = ('127.0.0.1',)(调试工具栏在“HTTP Headers”下显示REMOTE_ADDR ='127.0.0.1') - TEMPLATE_STRING_IF_INVALID = “(无效变量'%s'!)”
当渲染一个包含{{ sql_queries }} {{ debug }} 的模板时,输出结果是(无效变量'sql_queries'!)(无效变量'debug'!)
我的 Django 版本是 1.2.3。我错过了什么?
2个回答

3

在您的看法中,您是在创建一个Context还是RequestContext? 必须是RequestContext


嗯,我只是使用 render_to_response("some.template.file") - AndiDog
文档中包含以下内容:“如果您需要使用上下文处理器,请使用RequestContext实例呈现模板。您的代码可能如下所示:render_to_response('my_template.html',my_data_dictionary,context_instance = RequestContext(request))” - Ned Batchelder
好的,那帮助了我找到答案,非常感谢!必须显式传递'RequestContext'实例。 - AndiDog

1
Ned Batchelder的回答让我找到了正确的方向。在使用render_to_response时,必须明确传递一个RequestContext实例:
return render_to_response("some.template.file",
                          templateArguments,
                          context_instance = RequestContext(request))

从Django 1.3开始,您可以使用render函数作为速记:

return render(request, "some.template.file", templateArguments)

1
return render(request, "some.template.file", templateArguments) 更简洁,且默认使用 RequestContext - hobbes3
1
@hobbes3:没错,那是在1.3中引入的 - 我成长过程中使用的是旧版本。编辑了我的答案,谢谢! - AndiDog

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接