Package: django-require
django-requireis a Django staticfiles post-processor for optimizing
withRequireJS.FeaturesOptimize your static assets using the excellent r.js optimizer.Compile standalone modules using thealmond.jsshim.Compatible with any Django staticfiles storage backend.InstallationCheckout the latest django-require release and copy or symlink therequiredirectory into yourPYTHONPATH.  If using pip, runpip installdjango-require.Add'require'to yourINSTALLED_APPSsetting.Set yourSTATICFILES_STORAGEsetting to'require.storage.OptimizedStaticFilesStorage','require.storage.OptimizedCachedStaticFilesStorage'or'require.storage.OptimizedManifestStaticFilesStorage'.Available settingsAvailable settings, and their default values, are shown below. You
should configure this to match the layout of your project’s static
files. Please consult theRequireJSdocumentation for more information about how to build javascript using
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"Generating require.jsAs a shortcut to downloading a copy of require.js from the internet, you
can simply run therequire_initmanagement to copy a version of
require.js into yourSTATICFILES_DIRS, at the location specified by
yourREQUIRE_BASE_URLandREQUIRE_JSsettings.$./manage.pyrequire_initGenerating build profilesIn almost all cases, you’ll want to create a custom build profile for
your project. To help you get started, django-require can generate a
default build profile into yourSTATICFILES_DIRS. Just set yourREQUIRE_BUILD_PROFILEsetting to a build profile name, and runrequire_init. A good name for a build profile would be'app.build.js'.Any standalone modules that your specify with a build profile will also
have a default build profile generated when you run this command.Running javascript modules in templatesYou can run javascript modules in templates by using the{% require_module %}template tag.<html>{% load require %}<head>{% require_module 'main' %}</head><body></body></html>This template fragment would then render to something like:<html><head><scriptsrc="/static/js/require.js"data-main="/static/js/main.js"></script></head><body></body></html>If the'main'module was specified as a standalone module in yourREQUIRE_STANDALONE_MODULESsetting, andREQUIRE_DEBUGisFalse, then the template fragment would instead render as:This template fragment would then render to something like:<html><head><scriptsrc="/static/js/main-built.js"></script></head><body></body></html>Building standalone modulesAs a further optimization to your code, you can build your modules to
run independently of require.js, which can often speed up page load
times. Standalone modules are built using the almond.js shim, so consult
thealmond.jsdocumentation to
make sure that it’s safe to build your module in standalone mode.To specify standalone modules, simply add them to yourREQUIRE_STANDALONE_MODULESsetting, as below: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",}}Running the r.js optimizerThe r.js optimizer is run automatically whenever you call thecollectstaticmanagement command. The optimizer is run as a
post-processing step on your static files.django-require provides three storage classes that are ready to use with
the r.js optimizer:require.storage.OptimizedStaticFilesStorage- A filesystem-based
storage that runs the r.js optimizer.require.storage.OptimizedCachedStaticFilesStorage- As above, but
fingerprints all files with an MD5 hash of their contents for HTTP
cache-busting.require.storage.OptimizedManifestStaticFilesStorage- As above, but
fingerprints all files with an MD5 hash of their contents for HTTP
cache-busting and stores the fingerprints in a JSON file on disk instead
of using a cache. Please note that theOptimizedManifestStaticFilesStorageis only available in Django 1.7 and
above.Creating your own optimizing storage classesYou can add r.js optimization to any Django staticfiles storage class by
using therequire.storage.OptimizedFilesMixin. For example, to make
an optimizing storage that uploads to Amazon S3 usingS3BotoStoragefromdjango-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):passFor ready-made storage classes that combine django-require with Amazon S3, check outdjango-require-s3.Other projects extending django-requiredjango-require-license- prepends a license header to compressed file(s) generated with django-requireTestsYou can run the test suite from the root of the source checkout:test_project/manage.py test requireTest coverage reports can be generated from the same directory with:coverage run --source='.' test_project/manage.py test require
coverage htmlOpenhtmlcov/index.htmlin a browser to see the HTML coverage report.Support and announcementsDownloads and bug tracking can be found at themain project
website.You can keep up to date with the latest announcements by joining thedjango-require discussion
group.More informationThe django-require project was developed by Dave Hall. You can get the
code from thedjango-require project
site.Dave Hall is a freelance web developer, based in Cambridge, UK. You can
usually find him on the Internet in a number of different places:WebsiteTwitterGoogle Profile