django.contrib.staticfiles collects static files from each of your applications (and any other places you specify) into a single location that can easily be served in production.
For an introduction to the static files app and some usage examples, see 管理静态文件.
See staticfiles settings for details on the following settings:
django.contrib.staticfiles exposes three management commands.
Collects the static files into STATIC_ROOT.
Duplicate file names are by default resolved in a similar way to how template resolution works: the file that is first found in one of the specified locations will be used. If you’re confused, the findstatic command can help show you which files are found.
The collectstatic management command calls the post_process() method of the STATICFILES_STORAGE after each run and passes a list of paths that have been found by the management command. It also receives all command line options of collectstatic. This is used by the CachedStaticFilesStorage by default.
Some commonly used options are:
Do NOT prompt the user for input of any kind.
Ignore files or directories matching this glob-style pattern. Use multiple times to ignore more.
Do everything except modify the filesystem.
Clear the existing files before trying to copy or link the original file.
Create a symbolic link to each file instead of copying.
Don’t ignore the common private glob-style patterns 'CVS', '.*' and '*~'.
For a full list of options, refer to the commands own help by running:
$ python manage.py collectstatic --help
Searches for one or more relative paths with the enabled finders.
$ python manage.py findstatic css/base.css admin/js/core.js /home/special.polls.com/core/static/css/base.css /home/polls.com/core/static/css/base.css /home/polls.com/src/django/contrib/admin/media/js/core.js
By default, all matching locations are found. To only return the first match for each relative path, use the --first option:
$ python manage.py findstatic css/base.css --first /home/special.polls.com/core/static/css/base.css
This is a debugging aid; it’ll show you exactly which static file will be collected for a given path.
django-admin.py runserver --nostatic
Use the --insecure option to force serving of static files with the staticfiles app even if the DEBUG setting is False. By using this you acknowledge the fact that it’s grossly inefficient and probably insecure. This is only intended for local development, should never be used in production and is only available if the staticfiles app is in your project’s INSTALLED_APPS setting.
django-admin.py runserver --insecure
This method is called by the collectstatic management command after each run and gets passed the local storages and paths of found files as a dictionary, as well as the command line options.
The CachedStaticFilesStorage uses this behind the scenes to replace the paths with their hashed counterparts and update the cache appropriately.
A subclass of the StaticFilesStorage storage backend which caches the files it saves by appending the MD5 hash of the file’s content to the filename. For example, the file css/styles.css would also be saved as css/styles.55e7cbb9ba48.css.
The purpose of this storage is to keep serving the old files in case some pages still refer to those files, e.g. because they are cached by you or a 3rd party proxy server. Additionally, it’s very helpful if you want to apply far future Expires headers to the deployed files to speed up the load time for subsequent page visits.
The storage backend automatically replaces the paths found in the saved files matching other saved files with the path of the cached copy (using the post_process() method). The regular expressions used to find those paths (django.contrib.staticfiles.storage.CachedStaticFilesStorage.cached_patterns) by default cover the @import rule and url() statement of Cascading Style Sheets. For example, the 'css/styles.css' file with the content
would be replaced by calling the url() method of the CachedStaticFilesStorage storage backend, ultimatively saving a 'css/styles.55e7cbb9ba48.css' file with the following content:
To enable the CachedStaticFilesStorage you have to make sure the following requirements are met:
Since creating the MD5 hash can be a performance burden to your website during runtime, staticfiles will automatically try to cache the hashed name for each file path using Django’s caching framework. If you want to override certain options of the cache backend the storage uses, simply specify a custom entry in the CACHES setting named 'staticfiles'. It falls back to using the 'default' cache backend.
The method that is used when creating the hashed name of a file. Needs to return a hash for the given file name and content. By default it calculates a MD5 hash from the content’s chunks as mentioned above.
There are a few other helpers outside of the staticfiles app to work with static files:
This view function serves static files in development.
This view will only work if DEBUG is True.
That’s because this view is grossly inefficient and probably insecure. This is only intended for local development, and should never be used in production.
This view is automatically enabled by runserver (with a DEBUG setting set to True). To use the view with a different local development server, add the following snippet to the end of your primary URL configuration:
from django.conf import settings if settings.DEBUG: urlpatterns += patterns('django.contrib.staticfiles.views', url(r'^static/(?P<path>.*)$', 'serve'), )
Note, the beginning of the pattern (r'^static/') should be your STATIC_URL setting.
Since this is a bit finicky, there’s also a helper function that’ll do this for you:
This will return the proper URL pattern for serving static files to your already defined pattern list. Use it like this:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns # ... the rest of your URLconf here ... urlpatterns += staticfiles_urlpatterns()