【发布时间】:2014-05-20 10:10:07
【问题描述】:
我是畅销书Two Scoops of Django 的读者和忠实粉丝。 这本书充满了伟大的想法,但有一个对我来说不是很清楚。
作者建议在 Django 项目根目录中创建 static 和 templates 文件夹。template 文件夹用于“站点范围的模板”(在第 24 页说明)。
另外在第 162 页,他们说“模板通常应该放在 Django 项目的根文件夹中......唯一的例外是当您将应用程序捆绑到第三方包中时”。
他们没有在第 12 章中明确提及,但我想最好在 templates 根目录中为每个应用程序(与应用程序同名)创建一个文件夹。
假设在我的 icecreamratings Django 项目中,我有 2 个应用程序:
-
flavors有 2 个模板:new_flavor.html 和 details.html -
profiles有 1 个模板:details.html 所有模板都继承自base.html。
我猜他们会建议以下结构:
icecreamratings_project/
|-- ...
|-- icecreamratings/ # Django project root
|-- ...
|-- static/
|-- templates/
|-- 404.html
|-- 500.html
|-- base.html
|-- flavors/ # same name as app flavor
| |-- new_flavor.html
| |-- details.html
|-- profiles/ # same name as app profiles
|-- details.html
我没听错吗?
另一方面,Django official docs 建议在每个应用程序中创建一个static 文件夹,并在其中创建一个与应用程序同名的子文件夹,例如:my_app/static/my_app/myimage.jpg。
所以我们有 2 种不同的策略:一个唯一的 template 文件夹和许多 static 文件夹(每个应用程序中一个)。
拥有两种不同的策略显然是一个糟糕的选择。
那么,您如何看待在每个应用程序中存储 template 和 static 文件夹?
像这样的:
icecreamratings_project/
|-- ...
|-- icecreamratings/ # Django project root
|-- ...
|-- static/ # site-wide static files
| |-- bootstrap/ # bootstrap source files
| |-- jquery/ # jquery source files
| |-- css/ # css files used by base.html (and others in templates root)
| |-- js/ # javascript files used base.html (and others in templates root)
| |-- img/ # img files files used base.html (and others in templates root)
|-- templates/ # site-wide templates
| |-- 404.html
| |-- 500.html
| |-- base.html
|-- flavors/ # an app
|-- static/
| |-- flavors/ # static files specific for this app
| |-- css/ # css files for flavor app templates
| |-- js/ # javascript files for flavor app templates
| |-- img/ # img files for flavor app templates
|-- templates/ # template files specific for this app
|-- new_flavor.html
|-- details.html
【问题讨论】:
标签: django django-templates django-staticfiles