Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Templates Development vs. Production

Tags:

python

django

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?

like image 445
cusejuice Avatar asked Apr 30 '14 05:04

cusejuice


1 Answers

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.

like image 189
Jorge Leitao Avatar answered Oct 28 '22 19:10

Jorge Leitao