Django模板插件用于简化静态CDN资源连接。
django-cdnjs的Python项目详细描述
django cdnjs
django cdnjs是一个小的模板标记,它允许您加载外部cdn 按名称列出的资源。您可以下载任何所需的cdn,也可以只使用cdn 通过禁用/启用资源下载的URL。
工作原理
网站https://cdnjs.com对所有cdn都有一个API
存储在数据库中的存储库。这个api用于django-cdnjs
。
安装
安装PIP软件包
pip install django-cdnjs
将cdnjs
添加到已安装的应用程序:
INSTALLED_APPS = (
...
'cdnjs',
)
# Configure settings
CDNJS_STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'cdn')
CDNJS_STATIC_URL = '/static/cdn/' # With "/" at end of string
用法示例
如果您不提供存储库中的文件名,您需要哪个url,
django cdnjs将返回默认的存储库文件。例如font-awesome
默认文件是css/font-awesome.min.css
版本相同-django cdnjs将加载最新的可用版本,而not
始终稳定
default files.html 通常可以指定需要加载的版本。只需添加斜线
在存储库名称后指定版本。示例: 指定version.html 标签的第二个可选参数是应该选择到的文件
生成cdn url或本地uri。例如, 指定file.html 在这里你可以看到我在存储库名称上有一些错误。但是 无论如何,您应该提供两个django设置模块属性 默认情况下,{% load cdnjstag %}
<!DOCTYPE html>
...
<link rel="stylesheet" href="{% cdn "font-awesome" %}">
<script type="text/javascript" src="{% cdn "jquery" %}"></script>
...
{% 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>
...
boostrap
css的存储库
框架有css文件和js文件。cdnjs默认提供js文件,
所以我们需要手动指定需要哪个文件。{% 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>
...
cdnjs
api
按查询项bootstrap
返回结果,twitter-bootstrap
是第一个
其中之一。所以你可以打字。配置
必需选项
# This property uses not only for storing remote repositories,
# but for cdn urls cache too. So this option is required.
CDNJS_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. =)
CDNJS_STATIC_URL = '/static/cdn/' # With "/" at end of string
不加载远程存储库
cdnjs
下载要使用的远程存储库,而无需访问
远程资源。# If you need to use only local urli without CDN loading,
# just set this option to True
CDNJS_USE_LOCAL = True
# True - download remote repository and use local URI
# False - (default) do not download remote repository and use CDN URI
设置
Option Default Required Comment CDNJS_STATIC_ROOT None True Absolute path to the cdn static root. CDNJS_STATIC_URL None True Absolute path to the cdn static url. CDNJS_USE_LOCAL False False Should tag download requested repository and use local URI instead of CDN URI. 推荐PyPI第三方库