Задать вопрос

Тел: +7 965 3737 888

503

Просмотров

5

Ответов

Git media cache busting tag

<p>This tag appends the current git revision as a GET parameter to a media files so the web server can set an expires header far in the future. Your project must be structured such that MEDIA_ROOT/../.git exists.</p>
<p>Usage:
&lt;link rel="stylesheet" type="text/css" href="{% media myapp/css/base.css %}"&gt;</p>

Вопрос полезен? Да0/Нет0
file_3214.py(1.6Кб)
None

Ответы (5):

Ответkylefox:20.11.2009
Ответ полезен? Да0/Нет0

Why not just add {{ git_revision }} with a context processor? Seems less obstrusive to simply write ?v={{ git_revision }} in your templates, plus then you could use it elsewhere in your templates (like adding the revision to your site footer for superusers).

Anyway, cool idea. I think I'll adapt this concept some of my projects.

Ответadamlofts:19.11.2009
Ответ полезен? Да0/Нет0

@mk:

Thanks for the suggestion. Whats nice about your soln is that it doesn't rely on the project structure to get the .git folder. I guess my tag could be changed to just look for a .git folder up the directory hierarchy just as the git binary does.

As you mention, spawning a git process does have some shortcomings.

What I didn't mention in the description is the git_head_revision tag which is good for putting a version number on your website. e.g.

<p>Server running app version {% git_head_revision %}</p>

Ответmk:19.11.2009
Ответ полезен? Да0/Нет0

The same thing, a little bit shorter (depends on git being installed and executable by the webserver):

@register.simple_tag
def media_url(path):
    return os.path.join(
        settings.MEDIA_URL,
        path) + '?v=' + media_url.version

media_url.version = subprocess.Popen('git rev-parse --short HEAD',
    cwd=settings.MEDIA_ROOT,
    shell=True,
    stdout=subprocess.PIPE).communicate()[0]

Ответadamlofts:19.11.2009
Ответ полезен? Да0/Нет0

The tag will expand a line like this:

<link rel="stylesheet" type="text/css" href="{% media myapp/css/base.css %}">

into a line like:

<link rel="stylesheet" type="text/css" href="/path/to/media/root/myapp/css/base.css?a76s8d7a6s87d6s8a76ds">

Ответpbx:18.11.2009
Ответ полезен? Да0/Нет0

Cool. How about a usage example? (I.e. template code.)