2

In my base.html template which is imported in every Django template, I have a block with the Google Analytics script:

<!-- GOOGLE ANALYTICS -->

<script type="text/javascript">
    bla bla...
</script>

I would like to have this script only included when in production, but not during development. Is there a solution for this comparable to the solution in setting.py?

import socket

if socket.gethostname() == 'my-laptop':
    DEBUG = TEMPLATE_DEBUG = True
else:
    DEBUG = TEMPLATE_DEBUG = False

Anybody who knows of a template tag, or should I do my own one?

Meilo
  • 3,378
  • 3
  • 28
  • 34

5 Answers5

4

You could add your DEBUG variable to a context processor and just put an IF around that block. http://docs.djangoproject.com/en/dev/ref/templates/api/#subclassing-context-requestcontext

from django.conf import settings
def debug_context(request):
    return {'DEBUG': settings.DEBUG}

Then in your template:

{% if DEBUG %}
    STUFF
{% endif %}

Alternatively you could make the context processor return anything you want to key off of, anything in your settings file or otherwise.

Xealot
  • 1,659
  • 14
  • 26
  • Xealot, thank you for your advice which I implemented. Everything works fine after including the custom context processor in TEMPLATE_CONTEXT_PROCESSORS. Easiest solution to implement! – Meilo Nov 22 '10 at 21:06
  • Glad to hear, I've been using this particular implementation for 3+ years now and it's still the simplest I know of. Hopefully you'll start thinking of context processors to solve other problems now as well. – Xealot Nov 22 '10 at 21:10
3

In settings.py,Check debug is True, also add:

INTERNAL_IPS = (
    '127.0.0.1',
    'localhost',
)

Then you can use things in your template like this:

{% if debug %}
<span>This is debug</span>
{% else %}
<span>This is production</span>
{% endif %}

If want to change to production, set debug to False.

Ref: http://www.djangobook.com/en/2.0/chapter09.html

Chien-Wei Huang
  • 1,773
  • 1
  • 17
  • 27
0

Haven't seen such a template tag yet.

I'm more inclined to use different setting.py files for production and dev, and include analytics code as described in this answer.

This solution allows for a simpler template, and gives you the option to use different analytics code in different deployments of the same app.

Community
  • 1
  • 1
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
0

The template is probably not the best place to handle this.

usually, you'd write a context preprocessor that will process the request and add a flag to your context (such as PRODUCTION = True). Then you can use a simple if statement in the template, or better yet, write a simple custom tag that will hide that implementation detail from the template.

See also this answer.

Community
  • 1
  • 1
Arthur Debert
  • 10,237
  • 5
  • 26
  • 21
  • Arthur, thank you for you helpful answer! I choose the answer from Xealot since it helped me to implement my first ever context processor - not really difficult ;-) – Meilo Nov 22 '10 at 21:08
0

I do the following on lots of sites and it seems to work really well.

Then in my static media file directory I have a copy of base.css and base_dev.css. I do all my development in the base_dev.css file then merge it into base.css before I launch.

jtinsky
  • 66
  • 4