Serve various static files for development and production in Django

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

To serve them in production, I need to call them like

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

      

However, when developing, I want a regular css file to serve (this way I don't need to override and gzip every time I save):

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

      

Is there a way to achieve and automate this behavior by adding something to the deployer ?, is there still some other work to do (I could get rid of the .min extension if I could add .gz in pure form?

I want to point out that I know I can implement some kind of html parser that adds it for each deployment, but I'm looking for a neat and django oriented solution.

+3


source to share


3 answers


As usual, there is a Django package for that! I am using two:

django-compressor: http://django-compressor.readthedocs.org/en/latest/ django-pipeline: https://django-pipeline.readthedocs.org/en/latest/



I started working on the django pipeline but have recently switched to using a compressor. See docs, I believe this will be what you are looking for. Good luck!

+1


source


I love @Nursultan's idea. To ensure this, you can code the 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 %}

      

+1


source


I've never come across this problem, but I come up with these two solutions:

  • Use different settings.py

    for production and development. But this requires having the same names for *.min.js

    and changing the minifier config.
  • Or use a global variable and write everywhere

    {% if development_stage %} <link> {% else %} <link> {% endif %}

Django - How do I make a variable available to all templates?

0


source







All Articles