如何将我自己的文件添加到Django的“静态"文件夹
我已阅读 django静态文件文档,django的静态文件设置是这样的
I've read django static files document and made my django static files settings like this
setting.py
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')
STATIC_URL = '/static/'
html页面
<img src="{% static "admin/img/author.jpg" %}" alt="My image"/>
因此,如果我处理了django默认的静态文件之一,则可以正常工作.但是,如果我将自己的文件和文件夹添加到 static
文件夹中,则不会显示它.
So if I address one of django default static files, it works fine. But if I add my own file and folders to the static
folder, it doesn't show it.
我尝试过
python manage.py collectstatic
但是什么都没有改变.我该如何运作?
But nothing changed. How can I make it work?
几件事...
STATICFILES_DIRS = (
'path/to/files/in/development',
)
STATIC_ROOT = 'path/where/static/files/are/collected/in/production'
当 DEBUG = True
时,当您使用 {%static'path/to/file'%}
模板标记.
When DEBUG = True
, Django will automatically serve files located in any directories within STATICFILES_DIRS
when you use the {% static 'path/to/file' %}
template tag.
当 DEBUG = False
时,Django将自动不提供任何文件,并且您应从Apache,Nginx等在指定的位置提供这些文件. STATIC_ROOT
.
When DEBUG = False
, Django will not serve any files automatically, and you are expected to serve those files using Apache, Nginx, etc, from the location specified in STATIC_ROOT
.
当您运行 $ manage.py collectstatic
时,Django将复制位于 STATICFILES_DIRS
中的所有文件,以及在第三方应用程序中名为"static"的任何目录中的文件,放在 STATIC_ROOT
指定的位置.
When you run $ manage.py collectstatic
, Django will copy any and all files located in STATICFILES_DIRS
and also files within any directory named 'static' in 3rd party apps, into the location specified by STATIC_ROOT
.
我通常这样构造我的项目根目录:
I typically structure my project root as such:
my_project/
/static_assets/ (specified in STATICFILES_DIRS)
/js
/images
/static (specified in STATIC_ROOT)
我希望可以帮助您了解staticfiles应用程序的工作原理.
I hope that helps you understand how the staticfiles app works.