Skip to main content

A small extension to download css and js vendor files from CDNs and host them locally.

Project description

django-download-vendor-files

Setup

Install using pip:

pip install django-vendor-files

Add vendor_files to INSTALLED_APPS in your Django settings:

INSTALLED_APPS = (
    ...
    'vendor_files',
)

Put the vendor directory in STATICFILES_DIRS in your Django settings:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'vendor/'),
    ...
)

Add a VENDOR setting to your Django settings, and add your vendor files with URL and SNI, e.g.:

VENDOR = {
    'jquery': {
        'url': 'https://code.jquery.com/',
        'js': [
            {
                'path': 'jquery-3.2.1.min.js',
                'sri': 'sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=',
            }
        ]
    },
    'bootstrap': {
        'url': 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/',
        'js': [
            {
                'path': 'js/bootstrap.min.js',
                'sri': 'sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa',
            }
        ],
        'css': [
            {
                'path': 'css/bootstrap.min.css',
                'sri': 'sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u',
            }
        ]
    }
}

Usage

Put {% load vendor_tags %} at the top of your Django template and the vendor tag , e.g.: {% vendor 'jquery' %}, somewhere in your HTML:

{% load staticfiles %}
{% load vendor_tags %}

<!DOCTYPE html>
<html>
    <head>
        <title>{{ request.site.name }}</title>
        ...
        {% vendor 'jquery' %}
        {% vendor 'bootstrap' %}
    </head>

Then, you have 2 options:

  1. To use a CDN, put VENDOR_CDN = True in your Django settings and the vendor tag will be replaced by a <link> or <script> tag using the URLs in the settings.

  2. To use a local copy, put VENDOR_CDN = False (or nothing at all) in your Django settings and the vendor tag will be replaced by a <link> or <script> tag using your STATIC_PATH. The files can be downloaded:

    python ./manage.py download_vendor_files
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-vendor-files-0.1.1.tar.gz (3.9 kB view details)

Uploaded Source

File details

Details for the file django-vendor-files-0.1.1.tar.gz.

File metadata

File hashes

Hashes for django-vendor-files-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b278465c41950ce0a66fd7b8cf3b69108c10ee8e94dd0751d225b07b5d1ff7a4
MD5 910270871ec71199caee606d9d66903f
BLAKE2b-256 5daf9e6466b50c2e75c9279f9475bf91f692a52eb33497ec4c71e158a685eedb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page