【问题标题】:Where to store static/template files in Django according to "Two Scoops of Django"根据“Two Scoops of Django”在 Django 中存储静态/模板文件的位置
【发布时间】:2014-05-20 10:10:07
【问题描述】:

我是畅销书Two Scoops of Django 的读者和忠实粉丝。 这本书充满了伟大的想法,但有一个对我来说不是很清楚。

作者建议在 Django 项目根目录中创建 statictemplates 文件夹。
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 文件夹(每个应用程序中一个)。 拥有两种不同的策略显然是一个糟糕的选择。

那么,您如何看待在每个应用程序中存储 templatestatic 文件夹? 像这样的:

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


    【解决方案1】:

    django 官方文档不建议您在每个应用程序中创建静态文件夹。相反,它更喜欢将静态目录存储在项目目录下。

    Static file namespacing

    现在我们也许可以不用放置静态文件了 直接在 my_app/static/ 中(而不是创建另一个 my_app 子目录),但这实际上是一个坏主意。 Django 将使用 它找到的第一个名称匹配的静态文件,如果你有一个 在不同的应用程序 Django 中具有相同名称的静态文件 将无法区分它们。我们需要能够 将 Django 指向正确的位置,确保这一点的最简单方法是 通过命名它们。 就是把那些静态文件放在里面 另一个以应用程序本身命名的目录。

    官方文档也回答了你关于模板结构的问题。

    class app_directories.Loader

    从文件系统上的 Django 应用程序加载模板。 对于每个应用 INSTALLED_APPS,加载程序查找模板子目录。 如果 该目录存在,Django 会在其中查找模板。

    这意味着您可以将模板与您的各个应用程序一起存储。这 还可以轻松分发带有默认模板的 Django 应用程序。

    The Two Scoops of Django 是一本很棒的书。但是你应该先阅读官方文档。

    【讨论】:

      猜你喜欢
      • 2012-02-25
      • 2014-06-06
      • 2016-06-19
      • 2013-06-18
      • 1970-01-01
      • 2011-12-18
      • 2012-12-10
      • 1970-01-01
      • 2013-01-23
      相关资源
      最近更新 更多