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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With