How to check the TEMPLATE_DEBUG flag in a django template?

前端 未结 6 734
既然无缘
既然无缘 2020-12-04 15:31

Do you know if it is possible to know in a django template if the TEMPLATE_DEBUG flag is set?

I would like to disable my google analytics script when I am running my

6条回答
  •  生来不讨喜
    2020-12-04 15:48

    If modifying INTERNAL_IPS is not possible/suitable, you can do this with a context processor:

    in myapp/context_processors.py:

    from django.conf import settings
    
    def debug(context):
      return {'DEBUG': settings.DEBUG}
    

    in settings.py:

    TEMPLATE_CONTEXT_PROCESSORS = (
        ...
        'myapp.context_processors.debug',
    )
    

    Then in my templates, simply:

     {% if DEBUG %} .header { background:#f00; } {% endif %}
    

提交回复
热议问题