【发布时间】:2015-02-09 09:18:39
【问题描述】:
我有一个单独的模板中的谷歌分析代码,所以我可以在任何地方都包含它。
由于我不希望代码在开发中显示,我只是将其包装在 if 模板标签中:
... header of any template
{% include 'analytics.html' %}
</head>
.... rest of page
analytics.html 的内容:
{% if not debug %}
<script>
... analytics code
</script>
{% endif %}
在开发中,它按预期工作,分析代码从未显示。
但是,在生产中,分析代码只出现在主页中,在其他所有页面上它都保持隐藏。
这是我的 urls.py 的摘录(我正在使用 TemplateView):
url(r'^$', TemplateView.as_view(template_name="landing/home.html"), name='home'),
url(r'^prices/', TemplateView.as_view(template_name="landing/prices.html"), name='prices'),
url(r'^addons/', TemplateView.as_view(template_name="landing/addons.html"), name='addons'),
这些模板中的每一个都有{% include 'analytics.html' %},(我没有从一个共同的基础扩展它们,因为它们的设计差异太大)。
和我的模板上下文处理器:
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.request",
"allauth.account.context_processors.account",
"allauth.socialaccount.context_processors.socialaccount",
)
我在生产中将DEBUG 和TEMPLATE_DEBUG 设置为False。
我错过了什么?
【问题讨论】:
-
这个问题及其答案对您有帮助吗? stackoverflow.com/questions/1271631/…