Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serve different Static files on devel and production in Django

I have a production and local DJANGO development environment. To push things to production we have a deployer which minifies and gzips all CSS and JS files.

To serve them on production I need to call them like

  <link rel="stylesheet" href="{{ STATIC_URL }}css/filename.min.css.gz">

However on development I want the normal css file served (that way I don't have to re-minify and gzip each time I save) with:

  <link rel="stylesheet" href="{{ STATIC_URL }}css/filename.css">

Is there any way to achieve and automatize this behaviour by adding something to the deployer?, is there some other work-around (I could get rid of the .min extension if it's possible to add the .gz in a clean way?

I want to note the I know I could implement some html-parser which adds it on each deploy but I'm looking for a neat and django oriented solution.

like image 964
Pol Alvarez Vecino Avatar asked Sep 06 '25 03:09

Pol Alvarez Vecino


1 Answers

I like the @Nursultan idea. To enforce this you could code a context processor like this:

# On yourapp.context_processor.py
from django.conf import settings

def debug_set(request):
    return {'debug_set': settings.DEBUG}

# On your settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
    .
    .
    .
    'yourapp.context_processors.debug_set',
)

# On your templates
{% if debug_set %}
    <link rel="stylesheet" href="{{ STATIC_URL }}css/filename.css">
{% else %}
    <link rel="stylesheet" href="{{ STATIC_URL }}css/filename.min.css.gz">
{% endif %}
like image 140
Vladir Parrado Cruz Avatar answered Sep 07 '25 23:09

Vladir Parrado Cruz