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 django app on my development machine. Something like a {% if debug %} template tag would be perfect. Unfortunately, I didn’t find something like that in the documentation.

For sure, I can add this flag to the context but I would like to know if there is a better way to do that.

Assuming you haven’t set TEMPLATE_CONTEXT_PROCESSORS to some other value in, Django will automatically load the debug context preprocessor (as noted here). This means that you will have access to a variable called debug in your templates if settings.DEBUG is true and your local machine’s IP address (which can simply be is set in the variable settings.INTERNAL_IPS (which is described here). settings.INTERNAL_IPS is a tuple or list of IP addresses that Django should recognize as “internal”.

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

in myapp/

from django.conf import settings

def debug(context):
  return {'DEBUG': settings.DEBUG}



Then in my templates, simply:

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

Django 1.9+



{% if debug %} says:

A list of IP addresses, as strings, that:

  • Allow the debug() context processor to add some variables to the template context.

The debug context processor is in the default

If you haven’t already, it always helps to see if/how others have dealt with same issue on djangosnippets. The most recent snippet working with analytics tag is 1656:

What is nice about this solution is that it allows you to keep your GOOGLE_ANALYTICS_CODE = xxxxxx in in the case rest of your source is public, your key remains private. In addition it goes an extra step to not use analytics for logged in users.

Includes the Javascript for Google Analytics. Will not show Google Analytics code when DEBUG is on or to staff users.

Use {% googleanalyticsjs %} in your templates.

You must set something like


in your settings file.

Assumes ‘user’ in your template variables is request.user, which it will be if you use:

return render_to_response('template.html',{ }, context_instance=RequestContext(request))

(Assuming django.core.context_processors.auth is in TEMPLATE_CONTEXT_PROCESSORS, which it is by default)

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 + ""
      <script type="text/javascript">
          try {
          var pageTracker = _gat._getTracker('""" + str(code) + """');
      } catch(err) {}</script>

def googleanalyticsjs(parser, token):
  return ShowGoogleAnalyticsJS()

show_common_data = register.tag(googleanalyticsjs)

{% if debug %} can do the trick but only if you pass RequestContext instead of Context. Additionally, debug is not a boolean flag, its a function that when evaluated while DEBUG = True return some debugging information. This can be unnecessary overhead for your template.

Personally, I do this trick instead.

{% if request.META.HTTP_HOST == "" %}

This will always work but instead of relying on both DEBUG flag and INTERNAL_IP, it just work for the hard coded IP.

You will need to add the DEBUG flag to your context_processors.

There may not even be an alternative way. At least, none that I know of.