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:
-
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. -
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 yourSTATIC_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
Hashes for django-vendor-files-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0570f6ea83ff3f282415ef3e9197cb9d3b85c8ece3dbe2a8d194da8d803b281a |
|
MD5 | 8ca4e710821743a3f6c3811c2481a4cd |
|
BLAKE2b-256 | ddb4ab1462cfffaddf83fcb50df936abe6665813ef7da6e8c9d603dd58c9a55b |