4

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.

  • This would be easily managed by outputting the whole href via a template tag that does something different dependent on the value of a setting. – Daniel Roseman Jul 13 '15 at 20:41

3 Answers3

3

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 %}
Vladir Parrado Cruz
  • 2,301
  • 21
  • 27
1

As usual, there's a Django package for that! There are two I use:

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

I started on django-pipeline but have moved to using compressor as of late. See the docs, I believe one will be what you're looking for. Good luck!

FlipperPA
  • 13,607
  • 4
  • 39
  • 71
  • I'm already using dango compressor but I'm having this problem http://stackoverflow.com/questions/31389584/gulp-css-minify-gzip-django-compressor-not-working could they be related? anyway I've checked the documentation and I couldn't figure out the solution, could you be more precise @FlipperPA? – Pol Alvarez Vecino Jul 14 '15 at 12:18
  • In case someone needs more details as me: compressor automatically loads the .gz in production so no need to change names, just provide the gzipped files with same name – Pol Alvarez Vecino Jul 18 '15 at 15:34
  • Sorry for the delay in reply, I got tied up this week (actually, with helping run a Django conference). Yes, you can either let compressor take care of the GZIP'ing, or use gulp to compress and disable it in production for django-compressor. – FlipperPA Jul 19 '15 at 02:50
0

I have never met this problem, but I come up with these two solutions:

  1. Use different settings.py for production and development. But it requires to have the same names for *.min.js and change minifier's configuration.
  2. Or use a global variable and write everywhere

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

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

Community
  • 1
  • 1
Nursultan Zarlyk
  • 402
  • 1
  • 4
  • 16