cusejuice
cusejuice

Reputation: 10691

Django Templates Development vs. Production

I'm inheriting a product from someone who used Django and I have absolutely no idea how to use it.

What I'm trying to accomplish is to serve up different scripts in my base.html file, something like this:

<!-- if development -->
<script src="{% static "js/main.js" %}></script>
<! -- end -->

<!-- if production -->
<script src="{% static "production/js/main.min.js" %}></script>
<! -- end -->

The file structure is as follows:

app_name
|__ pages
|__ settings
|__ static
|__ templates
|__ etc

Inside the settings folder, there looks to be 3 files:

base.py : shared settings
development.py
production.py

Inside development.py,

from app_name.settings.base import *

DEBUG = True
TEMPLATE_DEBUG = DEBUG

// etc

I tried to do something like the following inside templates/base.html, but obviously not that easy.

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

Any help?

Upvotes: 5

Views: 1177

Answers (2)

isobolev
isobolev

Reputation: 1543

You need to make use of built in debug context processor. And don't forget to set INTERNAL_IPS setting (to ('127.0.0.1', ) for instance). After that:

{% if debug %}
STUFF
{% endif %}

should work.

Upvotes: 2

Jorge Leitao
Jorge Leitao

Reputation: 20173

You need to send your DEBUG in the settings to the templates.

The correct way to do that is to use context_processors, which populate the context of the template rendering with variables.

For your particular example, one way to solve it is to define a new module context_processors.py in your app_name, and write on it

from django.core.urlresolvers import resolve
from settings import DEBUG

def debug_settings(request):
    return {'DEBUG': DEBUG}

and in settings.py, use

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",
                               "app_name.context_processors.debug_settings",)

This will make all templates to see your DEBUG settings.

Upvotes: 3

Related Questions