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/'), 
    ...
)

Remarks:

  • stay first if you wont load vendor libs first
  • stay second after you own static if you wish ovveride (patch) vendor lib

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

VENDOR = {
    'jquery': {
        'url': 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0', # Not use '/' at end!
        'js': [
            {
                # finnaly url will be: https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js
                'path': 'jquery.min.js', 
                'sri': 'sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==',
            }
        ]
    },
    '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>

Extended tags, extract only js or css (can be usefull example for django-compressor):

{% vendor 'jquery' 'js' %}
# or you can use
{% vendor_js 'jquery' %}

{% vendor 'jquery' 'css' %}
# or you can use
{% vendor_css 'jquery' %}

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.2.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

django_vendor_files-0.2-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django-vendor-files-0.2.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.3

File hashes

Hashes for django-vendor-files-0.2.tar.gz
Algorithm Hash digest
SHA256 def933043bf9a993e613ed60b673aa51a7a3052c63dd281894cdda0d667718ed
MD5 cc05aead4584e6f7dd2059642a9fe670
BLAKE2b-256 dad40361fb26a7394fba3ed905bc1ed23806426573ae28bcb909ba5a4ed1d7d7

See more details on using hashes here.

File details

Details for the file django_vendor_files-0.2-py3-none-any.whl.

File metadata

  • Download URL: django_vendor_files-0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.3

File hashes

Hashes for django_vendor_files-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4122db09f68b9398b83a391f69ce91c0887b57fe29a6f39145a84b1a57a593ef
MD5 9da51a66c6e632029b2c7da1a3eccd82
BLAKE2b-256 c316e50941e9c05eca13b7022754ae2bd0ebd7814c1acf1d5dca400bbd058e80

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