Skip to main content

CDNJS API Django Template tag, which allows to simply use cdns.

Project description

Django CDNJS

Django CDNJS is the small template tag, that allows you to load external cdn resources by name. You can download any required CDN as well as use only CDN's URL by disabling/enabling resource downloading.

How it works

Website https://cdnjs.com has an API for all cdn repositories stored at database. This API was used in cdnjs.

Installation

Add cdnjs to your installed apps:

INSTALLED_APPS = (
    ...
    'cdnjs',
)

# Configure settings
CDN_STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'cdn')
CDN_STATIC_URL = '/static/cdn/' # With "/" at end of string

Usage example

If you will not provide filename from repository, which url you need, django-cdnjs will return default repository file. For example font-awesome default file is css/font-awesome.min.css

Same for version - django-cdnjs will load latest available version and not always stable

default-files.html

{% load cdnjstag %}

<!DOCTYPE html>
...

<link rel="stylesheet" href="{% cdn "font-awesome" %}">
<script type="text/javascript" src="{% cdn "jquery" %}"></script>
...

Usually you can specify which version you need to be loaded. Just add slash after repository name and specify version. Example:

specify-version.html

{% load cdnjstag %}

<!DOCTYPE html>
...

<link rel="stylesheet" href="{% cdn "font-awesome/4.7.0" %}">
<script type="text/javascript" src="{% cdn "jquery/3.2.1" %}"></script>
...

Second optional argument of cdn tag is the file which should be selected to build cdn-url or local-uri. For example, repository of the boostrap css framework has css-files as well as js. CDNJS provides js-file as the default, so we need to specify manually which file do we need.

specify-file.html

{% load cdnjstag %}

<!DOCTYPE html>
...

<link rel="stylesheet" href="{% cdn "bootstrap/3.3.7" "bootstrap.min.css" %}">
<script type="text/javascript" src="{% cdn "jquery/3.2.1" %}"></script>
<script type="text/javascript" src="{% cdn "bootstrap/3.3.7" "bootstrap.min.js" %}"></script>
...

And here you can see that I had some typo in repository name. But cdnjs API returns results by query term bootstrap and twitter-bootstrap is the first of them. So you can make typos.

Configuration

Required options

Anyway, you should provide two django settings module properties

# This property uses not only for storing remote repositories,
# but for cdn urls cache too. So this option is required. 
CDN_STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'cdn')

# This option is required, because I don't now why. You should
# know that it's so. Even if you using FORCE_CDN.
# If you want, you can contribute it and fix. =)
CDN_STATIC_URL = '/static/cdn/' # With "/" at end of string

Do not load remote repository

By default cdnjs downloads remote repository to be used without accessing remote resources.

# If you need to use only local urli without CDN loading,
# just set this option to True
CDN_USE_LOCAL = True

# True - download remote repository and use local URI
# False - (default) do not download remote repository and use CDN URI

Settings

Option Default Required Comment
CDN_STATIC_ROOT None True Absolute path to the cdn static root.
CDN_STATIC_URL None True Absolute path to the cdn static url.
CDN_USE_LOCAL False False Should tag download requested repository and use local URI instead of CDN URI.

Project details


Release history Release notifications | RSS feed

This version

1.4

Download files

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

Source Distribution

cdnjs-1.4.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

cdnjs-1.4-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file cdnjs-1.4.tar.gz.

File metadata

  • Download URL: cdnjs-1.4.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/49.1.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.7

File hashes

Hashes for cdnjs-1.4.tar.gz
Algorithm Hash digest
SHA256 bdd94967f63b850ec947f92833d3f6bb3b103449236440ec05a13b763fd9c18a
MD5 a96b199f368f2084e54c72980ff50abc
BLAKE2b-256 b5b829b468f55a44688634a993f44b755821caaff99e76b0424e1c7224798972

See more details on using hashes here.

File details

Details for the file cdnjs-1.4-py3-none-any.whl.

File metadata

  • Download URL: cdnjs-1.4-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/49.1.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.7

File hashes

Hashes for cdnjs-1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 77f0bc0ea6a439a1f01b95e5e9599b1e31f5ac2b9c7c9c4cfe4f6fa6e6c84322
MD5 3b0bc7e09ac7593e5db56f8f0d23fb7b
BLAKE2b-256 71a314edc8b5f3568dcd26eb9952d5ac77f4ab26b81c01f729fe6d5f0aada34c

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