如何在Django中构建静态应用文件? [英] How should I structure my static app files in Django?
问题描述
我正在寻找有关如何构建Django项目的建议.这是我目前的结构:
I'm looking for advise on how to structure up my Django project. This is my current structure at the moment:
project_name
- deploy
- app_name
-- migrations
-- templates
-- templatetags
-- __init__.py
-- admin.py
-- context_processors.py
-- defaults.py
-- models.py
-- tests.py
-- views.py
- static
-- admin
-- app_name
--- css
--- fonts
--- img
--- js
--- tinymce
- templates
- project_name
- __init__.py
- fabfile.py
- manage.py
我想知道将所有静态文件放在我的app文件夹中还是更好,还是现在将它们放在静态文件夹和app_name中呢?
I wonder if it's better to have all the static files in my app folder or should they be like it is at the moment in the static folder and app_name?
还是构建静态文件的另一种更好的方法?
Or is it another better way to structure my static files?
推荐答案
将它们放在应用程序目录下名为"static"的目录中.那就是 django.contrib.staticfiles.finders.AppDirectoriesFinder
在哪里寻找它们的
Put them in a directory named "static" under your app's directory. That's where django.contrib.staticfiles.finders.AppDirectoriesFinder
looks for them by default.
在 static/
目录中,使用您的应用程序名称创建一个目录.
Inside the static/
directory, create a directory with your app's name.
因此,在部署时,当您运行 ./manage.py collectstatic
时,Django会将所有文件复制到 settings.STATIC_ROOT
中定义的目录中,并且每个应用程序都会将文件放在自己的子目录中.
So on deployment, when you run ./manage.py collectstatic
, Django will copy all the files over to the directory defined in settings.STATIC_ROOT
and each app will have its files inside its own sub-directory.
project_name
- deploy
- app_name
- migrations
...
- static
- app_name
- css
- fonts
- img
- js
- tinymce
- templates
- app_name
- item.html
- list.html
- project_name
- __init__.py
- manage.py
此外,根据您拥有的字体,CSS和JS文件的数量,我只需将它们放在 app_name/static/app_name/
内,而不创建其他目录级别的 js/
和 css/
.正如他们所说,扁平比嵌套更好".
Also, depending on the number of fonts, CSS, and JS files you have, I'd simply put them inside app_name/static/app_name/
and not create another directory level js/
and css/
. "Flat is better than nested", as they say.
这篇关于如何在Django中构建静态应用文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!