Managing a static file using Django

I set a far-reaching future for my CSS / Javascript headers so that browsers no longer request files after they are cached. I also have a simple version control mechanism, so if the files are changed, clients will know.

Basically, I have a template tag, and I'm doing something like

<script type="text/javascript" src="{{ MEDIA_URL }}{% versioned "javascript/c/c.js" %}"></script>

which will become

<script type="text/javascript" src="http://x.com/media/javascript/c/c.min.js?123456"></script>.

The template tag opens a file javascript/c/c.js.vwhere it finds the version number and adds it to the query string. The version is generated by the shell script (it is now launched manually, a pre-commit hook may be added), which checks if the file has changed (with git diff).

Everything works fine, EXCEPT:

I want to implement the same type of versions for images. But images can reference CSS - this is a static file (served by nginx) - so there is no template tag there.

Which is better for file versioning?

Alternatively, I am thinking of replacing the template tag with middleware that changes all the links before returning the response. This is better than a template tag that can be mistakenly excluded. But still does not solve the problem of images referenced by CSS.

In addition, I know that having a version as part of a query string can cause problems with some proxies that do not cache the file, so I believe that part of the version is part of the file - for example javascript/c/c.123456.js.

. , Django (, CSS Django). , , nginx.

+5
5

pre-commit script, CSS.

, . -, , , .

!

0

, , Sass Compass. mixin, , . , ( compass watch, ).

- , python, , .

/var/www/aweso.me/
    ./files/
    ./private-files/
    ./static/
    ./project/
        ./manage.py
        ./fabfile.py
        ./.gitignore
        ./base/
            ./__init__.py
            ./wsgi.py
            ./settings/
                ./__init__.py
                ./modules
                    ./__init__.py
                    ./users.py
                    ./email.py
                    ./beta.py
                    ./redis.py
                    ./haystack.py
                ./version.py
                ./default.py
                ./local.py
                ./live.py

:

/var/www/aweso.me/project/base/settings/version.py

( ) git :

__version__ = "0763j34bf"

from .version import __version__ as ApplicationVersion settings.live from settings import ApplicationVersion .

+1

templatetag :

import os
import posixpath
import stat
import urllib

from django import template
from django.conf import settings
from django.contrib.staticfiles import finders

register = template.Library()

@register.simple_tag
def staticfile(path):
    normalized_path = posixpath.normpath(urllib.unquote(path)).lstrip('/')
    absolute_path = finders.find(normalized_path)
    if not absolute_path and getattr(settings, 'STATIC_ROOT', None):
        absolute_path = os.path.join(settings.STATIC_ROOT, path)
    if absolute_path:
        return '%s%s?v=%s' % (settings.STATIC_URL, path, os.stat(absolute_path)[stat.ST_MTIME])
    return path

pre 1.3 Django :

@register.simple_tag
def staticfile(path):
    file_path = os.path.join(settings.MEDIA_ROOT, path)
    url = '%s%s?v=%s' % (settings.MEDIA_URL, path, os.stat(file_path)[stat.ST_MTIME])
    return url

:

<link rel="stylesheet" href="{%  staticfile "css/style.css" %}" type="text/css" media="screen" />
0

, :

  • css Django.
  • Write a Django command to render your css templates (and save them anywhere)
  • In your script deployment, call this command.
0
source

It may also help: http://www.fanstatic.org/

-1
source

All Articles