Managing static files (e.g. images, JavaScript, CSS)
This page describes how you can serve these static files.
In your settings file, define
STATIC_URL
, for example:
- In your templates, use the template tag to build the URL forthe given relative path using the configured
STATICFILES_STORAGE
.
- {% load static %}
- <img src="{% static "my_app/example.jpg" %}" alt="My image">
- 将你的静态文件保存至你程序中名为
static
的目录中。例如``my_app/static/my_app/example.jpg
Serving the files
In addition to these configuration steps, you'll also need to actuallyserve the static files.
During development, if you use , this willbe done automatically by runserver
when is setto True
(see django.contrib.staticfiles.views.serve()
).
This method is grossly inefficient and probably insecure,so it is unsuitable for production.
See for proper strategies to servestatic files in production environments.
See the documentation for the STATICFILES_FINDERS
setting fordetails on how staticfiles
finds your files.
静态文件命名空间
Now we might be able to get away with putting our static files directlyin (rather than creating another my_app
subdirectory), but it would actually be a bad idea. Django will use thefirst static file it finds whose name matches, and if you had a static filewith the same name in a _different application, Django would be unable todistinguish between them. We need to be able to point Django at the rightone, and the easiest way to ensure this is by namespacing them. That is,by putting those static files inside another directory named for theapplication itself.
Serving static files during development
If you use django.contrib.staticfiles
as explained above, will do this automatically when DEBUG
is setto True
. If you don't have django.contrib.staticfiles
in, you can still manually serve static files using thedjango.views.static.serve()
view.
This is not suitable for production use! For some common deploymentstrategies, see .
For example, if your STATIC_URL
is defined as /static/
, you can dothis by adding the following snippet to your urls.py:
- from django.conf import settings
- urlpatterns = [
- # ... the rest of your URLconf goes here ...
- ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
注解
This helper function works only in debug mode and only ifthe given prefix is local (e.g. /static/
) and not a URL (e.g.).
During development, you can serve user-uploaded media files fromMEDIA_ROOT
using the view.
This is not suitable for production use! For some common deploymentstrategies, see Deploying static files.
For example, if your is defined as /media/
, you can dothis by adding the following snippet to your urls.py:
注解
This helper function works only in debug mode and only ifthe given prefix is local (e.g. /media/
) and not a URL (e.g.http://media.example.com/
).
测试中
When running tests that use actual HTTP requests instead of the built-intesting client (i.e. when using the built-in LiveServerTestCase
) the static assets need to be served alongthe rest of the content so the test environment reproduces the real one asfaithfully as possible, but LiveServerTestCase
has only very basic staticfile-serving functionality: It doesn't know about the finders feature of thestaticfiles
application and assumes the static content has already beencollected under .
Because of this, staticfiles
ships its owndjango.contrib.staticfiles.testing.StaticLiveServerTestCase
, a subclassof the built-in one that has the ability to transparently serve all the assetsduring execution of these tests in a way very similar to what we get atdevelopment time with DEBUG = True
, i.e. without having to collect themusing first.
django.contrib.staticfiles
provides a convenience management commandfor gathering static files in a single directory so you can serve them easily.
- Set the setting to the directory from which you'dlike to serve these files, for example:
- STATIC_ROOT = "/var/www/example.com/static/"
- Use a web server of your choice to serve thefiles. Deploying static files covers some common deploymentstrategies for static files.
了解更多
This document has covered the basics and some common usage patterns. Forcomplete details on all the settings, commands, template tags, and other piecesincluded in django.contrib.staticfiles
, see .