用RequireJS优化的Django StaticFiles后处理器。

django-require的Python项目详细描述


django require是用于优化的django staticfiles后处理器 用RequireJS

功能

  • 使用优秀的r.js优化器优化静态资产。
  • 使用 almond.js垫片。
  • 与任何django staticfiles存储后端兼容。

安装

  1. 检查最新的django require发行版并复制或符号链接 require目录到PYTHONPATH中。如果使用pip,运行 pip install django-require
  2. 'require'添加到INSTALLED_APPS设置中。
  3. STATICFILES_STORAGE设置设置为 'require.storage.OptimizedStaticFilesStorage''require.storage.OptimizedCachedStaticFilesStorage''require.storage.OptimizedManifestStaticFilesStorage'

可用设置

可用设置及其默认值如下所示。你 应该将其配置为与项目的静态布局相匹配 文件夹。请咨询RequireJS 有关如何使用 要求。

# The baseUrl to pass to the r.js optimizer, relative to STATIC_ROOT.REQUIRE_BASE_URL="js"# The name of a build profile to use for your project, relative to REQUIRE_BASE_URL.# A sensible value would be 'app.build.js'. Leave blank to use the built-in default build profile.# Set to False to disable running the default profile (e.g. if only using it to build Standalone# Modules)REQUIRE_BUILD_PROFILE=None# The name of the require.js script used by your project, relative to REQUIRE_BASE_URL.REQUIRE_JS="require.js"# A dictionary of standalone modules to build with almond.js.# See the section on Standalone Modules, below.REQUIRE_STANDALONE_MODULES={}# Whether to run django-require in debug mode.REQUIRE_DEBUG=settings.DEBUG# A tuple of files to exclude from the compilation result of r.js.REQUIRE_EXCLUDE=("build.txt",)# The execution environment in which to run r.js: auto, node or rhino.# auto will auto-detect the environment and make use of node if available and rhino if not.# It can also be a path to a custom class that subclasses# require.environments.Environment and defines some "args" function that# returns a list with the command arguments to execute.REQUIRE_ENVIRONMENT="auto"

生成require.js

作为从Internet下载Require.js副本的快捷方式,您可以 可以简单地运行require_init管理来复制 在您的STATICFILES_DIRS中的指定位置需要.js 您的REQUIRE_BASE_URLREQUIRE_JS设置。

$ ./manage.py require_init

生成生成配置文件

在几乎所有情况下,都需要为 你的项目。为了帮助您开始,django require可以生成 默认生成配置文件到您的STATICFILES_DIRS。把你的 REQUIRE_BUILD_PROFILE设置为生成配置文件名,然后运行 require_init。构建配置文件的好名字是 'app.build.js'

使用生成配置文件指定的任何独立模块也将 运行此命令时生成默认生成配置文件。

在模板中运行javascript模块

您可以使用 {% require_module %}模板标记。

<html>
    {% load require %}
    <head>
        {% require_module 'main' %}
    </head><body></body></html>

然后,此模板片段将呈现如下内容:

<html><head><scriptsrc="/static/js/require.js"data-main="/static/js/main.js"></script></head><body></body></html>

如果'main'模块被指定为 REQUIRE_STANDALONE_MODULES设置,REQUIRE_DEBUGFalse,则模板片段将呈现为:

然后,此模板片段将呈现如下内容:

<html><head><scriptsrc="/static/js/main-built.js"></script></head><body></body></html>

构建独立模块

作为对代码的进一步优化,您可以将模块构建为 独立于require.js运行,这通常可以加速页面加载 时代。独立模块是使用almond.js垫片构建的,因此请参考 将almond.js文档 确保以独立模式构建模块是安全的。

要指定独立模块,只需将它们添加到 REQUIRE_STANDALONE_MODULES设置,如下所示:

REQUIRE_STANDALONE_MODULES={"main":{# Where to output the built module, relative to REQUIRE_BASE_URL."out":"main-built.js",# Optional: A build profile used to build this standalone module."build_profile":"main.build.js",}}

运行r.js优化器

每当您调用 collectstatic管理命令。优化器作为 静态文件的后处理步骤。

django require提供了三个存储类,可以与 r.js优化器:

  • require.storage.OptimizedStaticFilesStorage-基于文件系统 运行r.js优化器的存储。
  • require.storage.OptimizedCachedStaticFilesStorage-如上所述,但是 使用MD5哈希对所有文件的内容进行HTTP指纹分析 破坏缓存。
  • require.storage.OptimizedManifestStaticFilesStorage-如上所述,但是 使用MD5哈希对所有文件的内容进行HTTP指纹分析 缓存破坏并将指纹存储在磁盘上的json文件中 使用缓存。请注意 OptimizedManifestStaticFilesStorage仅在django 1.7和 上面。

创建自己的优化存储类

可以通过以下方法将r.js优化添加到任何django staticfiles存储类 使用require.storage.OptimizedFilesMixin。例如,使 使用^{tt30}上传到amazon s3的优化存储$ 从 django-storages

fromstorages.backends.s3botoimportS3BotoStoragefromrequire.storageimportOptimizedFilesMixin# S3 storage with r.js optimization.classOptimizedS3BotoStorage(OptimizedFilesMixin,S3BotoStorage):pass# S3 storage with r.js optimization and MD5 fingerprinting.fromdjango.contrib.staticfiles.storageimportCachedFilesMixinclassOptimizedCachedS3BotoStorage(OptimizedFilesMixin,CachedFilesMixin,S3BotoStorage):pass

对于结合了django需求和amazon s3的现成存储类,请查看 django-require-s3

延长Django Requ的其他项目IRE

测试

您可以从源签出的根目录运行测试套件:

test_project/manage.py test require

测试覆盖率报告可以从同一目录中生成:

coverage run --source='.' test_project/manage.py test require
coverage html

在浏览器中打开html coverage报告。

支持和公告

下载和错误跟踪可以在main project website找到。

您可以通过加入 django-require discussion group

更多信息

django需求项目是由dave hall开发的。你可以得到 来自django-require project site的代码。

Dave Hall是一名自由网络开发人员,总部位于英国剑桥。你可以 通常在网上的许多不同的地方可以找到他:

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

推荐PyPI第三方库


热门话题
安卓无法在我的适配器中调用自定义添加的方法(扩展了RecyclerView.adapter<MyAdapter.ViewHolder>)   java我关闭我的intellij想法,选择[断开连接]   java中字节数组对象的编码和解码   如何在Java中调用匿名类方法   java Hibernate/HSQLDB:用户缺乏创建约束的权限   在JSP中使用JAVA将SQL数据追加到数据表中   java DOCX4J:向表中添加最后一行   java spring webflux:如何从同步调用发布事件以进行异步处理?   java joda time api在两组不同的日期中表现不同   java使用HttpClient获取URL状态代码的最快方法是什么   Java:字符串格式   java如何从另一个对象访问服务中的变量   java截取发送请求而不进行编码   classpath Java:将包含的Jar文件添加到linux中创建的Jar文件中   java如何立即终止socketIO操作上的线程阻塞?