|
| 1 | +原文:[Managing static files (e.g. images, JavaScript, CSS)](https://docs.djangoproject.com/en/1.9/howto/static-files/) |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +网站通常需要提供额外的文件,例如图片,JavaScript, 或者CSS。在Django中,我们将这些文件称之为“静态文件”。Django提供了[`django.contrib.staticfiles`](https://docs.djangoproject.com/en/1.9/ref/contrib/staticfiles/#module-django.contrib.staticfiles "django.contrib.staticfiles:An app for handling static files." )来帮助你管理它们。 |
| 6 | + |
| 7 | +本文描述了你可以如何提供这些静态文件。 |
| 8 | + |
| 9 | +## 配置静态文件 |
| 10 | + |
| 11 | + 1. 确保在你的[`INSTALLED_APPS`](https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-INSTALLED_APPS)中包含了`django.contrib.staticfiles`。 |
| 12 | + |
| 13 | + 2. 在你的settings文件中,定义[`STATIC_URL`](https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-STATIC_URL),例如: |
| 14 | + |
| 15 | + ```python |
| 16 | + STATIC_URL = '/static/' |
| 17 | + ``` |
| 18 | + |
| 19 | + 3. 在你的模板中,要么像`/static/my_app/myexample.jpg`这样硬编码url,要么,更好的做法是,使用[`static`](https://docs.djangoproject.com/en/1.9/ref/contrib/staticfiles/#std:templatetag-staticfiles-static)模板标签通过使用配置的[`STATICFILES_STORAGE`](https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-STATICFILES_STORAGE) 存储(当你想要转到一个内容分发网络(CDN)来提供静态文件时,这会容易得多),为给定的相对路径建立url。 |
| 20 | + |
| 21 | + ```html |
| 22 | + {% load staticfiles %} |
| 23 | + <img src="{% static "my_app/myexample.jpg" %}" alt="My image"/> |
| 24 | + ``` |
| 25 | + |
| 26 | + 4. 将你的静态文件保存在你的应用中的名为`static`的文件夹下。例如,`my_app/static/my_app/myimage.jpg`。 |
| 27 | + |
| 28 | +>提供文件 |
| 29 | + |
| 30 | +>除了这些配置步骤外,你还会需要实际的提供静态文件。 |
| 31 | + |
| 32 | +>部署期间,如果你使用[`django.contrib.staticfiles`](https://docs.djangoproject.com/en/1.9/ref/contrib/staticfiles/#module-django.contrib.staticfiles "django.contrib.staticfiles: An app for handling static files." ),那么当设置[`DEBUG`](https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-DEBUG)为`True`时,[`runserver`](https://docs.djangoproject.com/en/1.9/ref/django-admin/#django-admin-runserver)将会自动完成 (见[`django.contrib.staticfiles.views.serve()`](https://docs.djangoproject.com/en/1.9/ref/contrib/staticfiles/#django.contrib.staticfiles.views.serve "django.contrib.staticfiles.views.serve" ))。 |
| 33 | + |
| 34 | +>这种方法是**非常低效的**,并且可能**不安全**,所以**不适用于生产**。 |
| 35 | + |
| 36 | +>见[_部署静态文件_](https://docs.djangoproject.com/en/1.9/howto/static-files/deployment/),以获得在生产环境中提供静态文件的正确策略。 |
| 37 | + |
| 38 | +你的工程也有可能拥有一些不特定于某个应用的静态文件。除了使用你的应用中的`static/`目录,你还可以在你的settings文件中定义目录列表([`STATICFILES_DIRS`](https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-STATICFILES_DIRS)),Django将会查找该列表以搜索静态文件。例如: |
| 39 | + |
| 40 | + ```python |
| 41 | + STATICFILES_DIRS = [ |
| 42 | + os.path.join(BASE_DIR, "static"), |
| 43 | + '/var/www/static/', |
| 44 | + ] |
| 45 | + ``` |
| 46 | + |
| 47 | +见[`STATICFILES_FINDERS`](https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-STATICFILES_FINDERS)设置项的文档,以获得`staticfiles`如何查找你的文件的相关细节。 |
| 48 | + |
| 49 | +>静态文件名字空间 |
| 50 | +
|
| 51 | +>现在,直接把我们的静态文件放到`my_app/static/` (而不是创建另一个`my_app`子目录)中也许可以蒙混过关,但这其实是一个糟糕的想法。Django将会使用它所找到的第一个名字匹配的静态文件,而如果另一个不同的应用中有相同名字的静态文件,那么Django将无法区分它们。我们需要能够让Django指向正确的静态文件,而最简单的方式就是使用名字空间。也就是说,将那些静态文件放到另一个由应用自身命名的目录中。 |
| 52 | +
|
| 53 | +## 在部署期间提供静态文件 |
| 54 | + |
| 55 | +If you use [`django.contrib.staticfiles`](https://docs.djangoproject.com/en/1.9/ref/contrib/staticfiles/#module-django.contrib.staticfiles"django.contrib.staticfiles: An app for handling static files." ) as explained above, [`runserver`](https://docs.djangoproject.com/en/1.9/ref/django-admin/#django-admin-runserver) will do this automatically when[`DEBUG`](https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-DEBUG) is set to `True`. If you don't have `django.contrib.staticfiles` in [`INSTALLED_APPS`](https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-INSTALLED_APPS), you can still manually serve static files using the [`django.contrib.staticfiles.views.serve()`](https://docs.djangoproject.com/en/1.9/ref/contrib/staticfiles/#django.contrib.staticfiles.views.serve"django.contrib.staticfiles.views.serve" ) view. |
| 56 | + |
| 57 | +This is not suitable for production use! For some common deployment |
| 58 | +strategies, see [_部署静态文件_](https://docs.djangoproject.com/en/1.9/howto/static-files/deployment/)。 |
| 59 | + |
| 60 | +例如,如果定义[`STATIC_URL`](https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-STATIC_URL)为`/static/`,那么你可以通过添加下面的代码段到你的urls.py中来做到这点: |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + from django.conf import settings |
| 65 | + from django.conf.urls.static import static |
| 66 | + |
| 67 | + urlpatterns = [ |
| 68 | + # ... the rest of your URLconf goes here ... |
| 69 | + ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) |
| 70 | + |
| 71 | + |
| 72 | +>Note |
| 73 | +
|
| 74 | +>This helper function works only in debug mode and only if the given prefix is local (e.g. `/static/`) and not a URL (e.g. `http://static.example.com/`). |
| 75 | +
|
| 76 | +>Also this helper function only serves the actual |
| 77 | +[`STATIC_ROOT`](https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-STATIC_ROOT) folder; it doesn't perform static files discovery like [`django.contrib.staticfiles`](https://docs.djangoproject.com/en/1.9/ref/contrib/staticfiles/#module-django.contrib.staticfiles "django.contrib.staticfiles:An app for handling static files." ). |
| 78 | + |
| 79 | +## 在开发过程中提供用户上传的文件 |
| 80 | + |
| 81 | +During development, you can serve user-uploaded media files from |
| 82 | +[`MEDIA_ROOT`](https://docs.djangoproject.com/en/1.9/ref/settings/#std |
| 83 | +:setting-MEDIA_ROOT) using the [`django.contrib.staticfiles.views.serve()`](ht |
| 84 | +tps://docs.djangoproject.com/en/1.9/ref/contrib/staticfiles/#django.contrib.st |
| 85 | +aticfiles.views.serve "django.contrib.staticfiles.views.serve" ) view. |
| 86 | + |
| 87 | +This is not suitable for production use! For some common deployment |
| 88 | +strategies, see [_Deploying static |
| 89 | +files_](https://docs.djangoproject.com/en/1.9/howto/static-files/deployment/). |
| 90 | + |
| 91 | +For example, if your |
| 92 | +[`MEDIA_URL`](https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting- |
| 93 | +MEDIA_URL) is defined as `/media/`, you can do this by adding the following |
| 94 | +snippet to your urls.py: |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + from django.conf import settings |
| 99 | + from django.conf.urls.static import static |
| 100 | + |
| 101 | + urlpatterns = [ |
| 102 | + # ... the rest of your URLconf goes here ... |
| 103 | + ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) |
| 104 | + |
| 105 | + |
| 106 | +Note |
| 107 | + |
| 108 | +This helper function works only in debug mode and only if the given prefix is |
| 109 | +local (e.g. `/media/`) and not a URL (e.g. `http://media.example.com/`). |
| 110 | + |
| 111 | +## 测试 |
| 112 | + |
| 113 | +When running tests that use actual HTTP requests instead of the built-in |
| 114 | +testing client (i.e. when using the built-in [`LiveServerTestCase`](https://do |
| 115 | +cs.djangoproject.com/en/1.9/topics/testing/tools/#django.test.LiveServerTestCa |
| 116 | +se "django.test.LiveServerTestCase" )) the static assets need to be served |
| 117 | +along the rest of the content so the test environment reproduces the real one |
| 118 | +as faithfully as possible, but `LiveServerTestCase` has only very basic static |
| 119 | +file-serving functionality: It doesn't know about the finders feature of the |
| 120 | +`staticfiles` application and assumes the static content has already been |
| 121 | +collected under |
| 122 | +[`STATIC_ROOT`](https://docs.djangoproject.com/en/1.9/ref/settings/#std |
| 123 | +:setting-STATIC_ROOT). |
| 124 | + |
| 125 | +Because of this, `staticfiles` ships its own [`django.contrib.staticfiles.test |
| 126 | +ing.StaticLiveServerTestCase`](https://docs.djangoproject.com/en/1.9/ref/contr |
| 127 | +ib/staticfiles/#django.contrib.staticfiles.testing.StaticLiveServerTestCase |
| 128 | +"django.contrib.staticfiles.testing.StaticLiveServerTestCase" ), a subclass of |
| 129 | +the built-in one that has the ability to transparently serve all the assets |
| 130 | +during execution of these tests in a way very similar to what we get at |
| 131 | +development time with `DEBUG = True`, i.e. without having to collect them |
| 132 | +using [`collectstatic`](https://docs.djangoproject.com/en/1.9/ref/contrib/stat |
| 133 | +icfiles/#django-admin-collectstatic) first. |
| 134 | + |
| 135 | +## 部署 |
| 136 | + |
| 137 | +[`django.contrib.staticfiles`](https://docs.djangoproject.com/en/1.9/ref/contr |
| 138 | +ib/staticfiles/#module-django.contrib.staticfiles "django.contrib.staticfiles: |
| 139 | +An app for handling static files." ) provides a convenience management command |
| 140 | +for gathering static files in a single directory so you can serve them easily. |
| 141 | + |
| 142 | + 1. Set the [`STATIC_ROOT`](https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-STATIC_ROOT) setting to the directory from which you'd like to serve these files, for example: |
| 143 | + |
| 144 | + STATIC_ROOT = "/var/www/example.com/static/" |
| 145 | + |
| 146 | + |
| 147 | + 2. Run the [`collectstatic`](https://docs.djangoproject.com/en/1.9/ref/contrib/staticfiles/#django-admin-collectstatic) management command: |
| 148 | + |
| 149 | + $ python manage.py collectstatic |
| 150 | + |
| 151 | + |
| 152 | +This will copy all files from your static folders into the |
| 153 | +[`STATIC_ROOT`](https://docs.djangoproject.com/en/1.9/ref/settings/#std |
| 154 | +:setting-STATIC_ROOT) directory. |
| 155 | + |
| 156 | + 3. Use a web server of your choice to serve the files. [_Deploying static files_](https://docs.djangoproject.com/en/1.9/howto/static-files/deployment/) covers some common deployment strategies for static files. |
| 157 | + |
| 158 | +## 了解更多 |
| 159 | + |
| 160 | +This document has covered the basics and some common usage patterns. For |
| 161 | +complete details on all the settings, commands, template tags, and other |
| 162 | +pieces included in [`django.contrib.staticfiles`](https://docs.djangoproject.com/en/1.9/ref/contrib/staticfiles/#module-django.contrib.staticfiles"django.contrib.staticfiles: An app for handling static files." ), see [_thestaticfilesreference_](https://docs.djangoproject.com/en/1.9/ref/contrib/staticfiles/). |
| 163 | + |
0 commit comments