Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally loading development or production static files in Django template

I am using Yeoman (http://yeoman.io/) as a front-end build process which concat/minifies css and javascript.

In a development environment I want separate, un-minified source to be loaded for easy debugging without having to setup Chrome Source Maps (http://code.google.com/p/closure-compiler/wiki/SourceMaps). In production the concatenated, minified source is loaded for performance.

My initial approach is to use a conditional inside my template as follows:

    {% if DEVELOPEMENT %}
        <!-- library -->
        <script src="{{ STATIC_URL }}lib/jquery.js"></script>
        <script src="{{ STATIC_URL }}lib/some_library.js"></script>

        <!-- app -->
        <script src="{{ STATIC_URL }}scripts/main.js"></script>
        <script src="{{ STATIC_URL }}scripts/app_model.js"></script>
        <script src="{{ STATIC_URL }}scripts/app_view.js"></script>

    {% else %}
        <script src="{{ STATIC_URL }}min/lib.min.js"></script>
        <script src="{{ STATIC_URL }}min/app.min.js"></script>
    {% endif %}

DEVELOPMENT is exposed to the template context using a context processor:

from django.conf import settings # import the settings file

def development(context):
    return {'DEVELOPEMENT': settings.DEVELOPEMENT}

Are there any drawbacks to this method and is there a cleaner way to accomplish this in Django?

like image 512
mmartin Avatar asked Dec 30 '25 17:12

mmartin


1 Answers

I would use your DEBUG setting which is already included in django.core.context_processors.debug.

{% if debug %}
    <!-- scripts -->
{% else %}
    <!-- other scripts -->
{% endif %}
like image 88
drewman Avatar answered Jan 01 '26 11:01

drewman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!