Git Product home page Git Product logo

metametricsinc / django-static-version Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 1.0 31 KB

This is a simple django utility that adds a version number to static files with a custom templatetag and context_processor. The version number is pulled from the django settings. The motivating use case was cache busting in CloudFront, we will simple bump up the version number on redeploy to invalidate static files.

License: BSD 2-Clause "Simplified" License

HTML 4.89% Python 95.11%

django-static-version's Introduction

Static version

This is a simple django utility that adds a version number to static files with a custom templatetag and context_processor. The version number is pulled from the django settings. The motivating use case was cache busting in CloudFront, we will simple bump up the version number on redeploy to invalidate static files.

How to use

  • Install this package.
pip install django-static-version
  • Add the setting for STATIC_VERSION.
STATIC_VERSION="1.0.1"
  • Add the context processor in settings. This adds the setting above to the request context.
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'static_version.context_processors.static_urls'
            ],
        },
    },
]
  • Use the custom filter or tag to add the version number to url query strings. The filter version is available as follows:
{{ "http://fake.com/"|version:static_version }}

The above code block will add our version number, 1.0.1, as the query string ?v=1.0.1 to the end of the url http://fake.com/ resulting in http://fake.com/?v=1.0.1

Alternately we can use the custom template tag static_version.

{% static_version 'bogus.jpg' %}

The above code will pull the version from the context and append it to the url returned by the built-in static tag and append the version number as above. This will result in /static/bogus.jpg?v=1.2 if your static root is /static/.

Tests

To run tests you simple run django-admin with the test_settings file

django-admin.py test --settings=test_settings

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.