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

{% load cdnjstag %}

<!DOCTYPE html>
...

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

通常可以指定需要加载的版本。只需添加斜线 在存储库名称后指定版本。示例:

指定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>
...

标签的第二个可选参数是应该选择到的文件 生成cdn url或本地uri。例如,boostrapcss的存储库 框架有css文件和js文件。cdnjs默认提供js文件, 所以我们需要手动指定需要哪个文件。

指定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>
...

在这里你可以看到我在存储库名称上有一些错误。但是cdnjsapi 按查询项bootstrap返回结果,twitter-bootstrap是第一个 其中之一。所以你可以打字。

配置

必需选项

无论如何,您应该提供两个django设置模块属性

# 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

设置

OptionDefaultRequiredComment
CDNJS_STATIC_ROOTNoneTrueAbsolute path to the cdn static root.
CDNJS_STATIC_URLNoneTrueAbsolute path to the cdn static url.
CDNJS_USE_LOCALFalseFalseShould tag download requested repository and use local URI instead of CDN URI.

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
如何获得@sign,以便在Java代码中键入@override?   java Facebook登录不起作用,我忘了什么?   json如何在java中从MongoDB Atlas获取特定字段?   java如何在Android上的JNI中实现委托/协议(在iOS上)?   java为什么这个循环多次的程序在循环后有一个“println”时需要花费时间?   java无法使组合框正常工作   JavaCQ5。如何为作者显示列表发布者?版本5.5   java我可以要求泛型参数具有泛型参数吗?   JavaOKHTTP无法获取整个JSON   数组Java:用字符减去字符意味着什么?   java为什么Eclipse content assist无法从部分方法名生成方法存根?   java使用线程在Android Studio中一次加载一个多位图   java遍历二叉树并返回一个值