我在單獨的模板中使用了谷歌分析代碼,因此我可以將它包含在任何地方。Django僅在生產中包含谷歌分析代碼,但未在開發中
,因爲我不想要的代碼在發展中顯現,我只是把它包在if
templatetag:analytics.html的
... header of any template
{% include 'analytics.html' %}
</head>
.... rest of page
內容:
{% 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' %}
,(我還沒有從一個共同的基礎,因爲他們擴展他們在設計上變化太大)。
和我的模板context處理器:
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
生產。
我錯過了什麼?
請問這個問題和答案對你有幫助嗎? http://stackoverflow.com/questions/1271631/how-to-check-the-template-debug-flag-in-a-django-template – LaundroMat 2017-01-27 13:33:33