luc*_*luc 58 python django templates
你知道是否可以在django模板中知道是否设置了TEMPLATE_DEBUG标志?
我想在我的开发机器上运行我的django应用程序时禁用我的谷歌分析脚本.像{%if debug%}模板标签这样的东西是完美的.不幸的是,我在文档中没有找到类似的东西.
当然,我可以将此标志添加到上下文中,但我想知道是否有更好的方法来执行此操作.
mip*_*adi 70
假如您没有设置TEMPLATE_CONTEXT_PROCESSORS到其他值settings.py,Django会自动加载的debug情况下预处理器(如注意这里).这意味着如果为true ,您将可以访问debug模板中调用的变量,并且在变量中设置了本地计算机的IP地址(可以简单地为127.0.0.1)(此处描述).是Django应该识别为"内部"的元组或IP地址列表. settings.DEBUGsettings.INTERNAL_IPSsettings.INTERNAL_IPS
fre*_*ley 53
如果INTERNAL_IPS无法进行修改/适合,则可以使用上下文处理器执行此操作:
在myapp/context_processors.py:
from django.conf import settings
def debug(context):
return {'DEBUG': settings.DEBUG}
Run Code Online (Sandbox Code Playgroud)
在settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
...
'myapp.context_processors.debug',
)
Run Code Online (Sandbox Code Playgroud)
然后在我的模板中,简单地说:
{% if DEBUG %} .header { background:#f00; } {% endif %}
Run Code Online (Sandbox Code Playgroud)
Cir*_*四事件 19
Django 1.9
settings.py:
INTERNAL_IPS = (
'127.0.0.1',
)
Run Code Online (Sandbox Code Playgroud)
模板:
{% if debug %}
Run Code Online (Sandbox Code Playgroud)
https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-INTERNAL_IPS说:
作为字符串的IP地址列表:
- 允许debug()上下文处理器将一些变量添加到模板上下文中.
所述debug上下文处理器处于默认settings.py.
如果你还没有,那么看看/如何在djangosnippets处理同样的问题总是有帮助的.使用分析标记的最新代码段是1656:http: //www.djangosnippets.org/snippets/1656/
这个解决方案有什么GOOGLE_ANALYTICS_CODE = xxxxxx好处,它允许你保持你的local_settings.py在你的源的其余部分是公开的,你的密钥保持私密.此外,它还向登录用户不再使用分析.
包含适用于Google Analytics的Javascript.在启用DEBUG时或在向员工用户显示时,不会显示Google Analytics代码.
{% googleanalyticsjs %}在模板中使用.你必须设置类似的东西
Run Code Online (Sandbox Code Playgroud)GOOGLE_ANALYTICS_CODE = "UA-1234567-1"在您的设置文件中.
假设您的模板变量中的'user',
request.user如果您使用它将是:Run Code Online (Sandbox Code Playgroud)return render_to_response('template.html',{ }, context_instance=RequestContext(request))(假设
django.core.context_processors.auth是在TEMPLATE_CONTEXT_PROCESSORS,默认情况下)
Run Code Online (Sandbox Code Playgroud)from django import template import settings register = template.Library() class ShowGoogleAnalyticsJS(template.Node): def render(self, context): code = getattr(settings, "GOOGLE_ANALYTICS_CODE", False) if not code: return "<!-- Goggle Analytics not included because you haven't set the settings.GOOGLE_ANALYTICS_CODE variable! -->" if 'user' in context and context['user'] and context['user'].is_staff: return "<!-- Goggle Analytics not included because you are a staff user! -->" if settings.DEBUG: return "<!-- Goggle Analytics not included because you are in Debug mode! -->" return """ <script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> try { var pageTracker = _gat._getTracker('""" + str(code) + """'); pageTracker._trackPageview(); } catch(err) {}</script> """ def googleanalyticsjs(parser, token): return ShowGoogleAnalyticsJS() show_common_data = register.tag(googleanalyticsjs)
{% if debug %}可以做到这一点,但前提是你通过RequestContext而不是Context. 此外,debug不是布尔标志,它是一个函数,在评估时DEBUG = True返回一些调试信息。这对于您的模板来说可能是不必要的开销。
就个人而言,我会做这个把戏。
{% if request.META.HTTP_HOST == "127.0.0.1:8000" %}
Run Code Online (Sandbox Code Playgroud)
这将始终有效,但它不依赖于 DEBUG 标志和 INTERNAL_IP,它只适用于硬编码的 IP。
| 归档时间: |
|
| 查看次数: |
27943 次 |
| 最近记录: |