【问题标题】:where to keep static files in django application, How to server static files from more than one location from nginx在 django 应用程序中保存静态文件的位置,如何从 nginx 的多个位置提供静态文件
【发布时间】:2014-11-19 00:48:25
【问题描述】:

我有一个 django 应用程序,使用 nginx 提供静态文件。我想在项目中包含应用(功能)。

我想创建 newapp 并将文件放在静态文件夹下。 (但我需要更改 nginx conf 以提供这些目录中的文件)

project
 |
 |-templates
 |-static
    |
    |-js
    |-css


 newapp
 |
 |-static
    |
    |-js
    |-css
  1. 这是保留静态文件、模板的首选方式(在应用程序中?(在该应用程序下进行了测试,我觉得更清楚))

  2. 如果我们想从两个位置提供静态文件如何更改 nginx conf?

我当前的 nginx 配置是这样的

location /static/ {  alias /myproject/location/static/; }
  1. 如果我们将所有文件(静态、模板)都放在那个主项目中,维护起来会不会变得复杂?

  2. 有没有办法匹配像

    别名 /myproject/*/static

【问题讨论】:

    标签: python django nginx


    【解决方案1】:

    这正是我们拥有staticfiles app 的原因。将每个应用程序的资产放在该应用程序中,然后在部署时运行manage.py collectstatic,它们将全部收集在 /static/ 下的子目录中

    【讨论】:

      【解决方案2】:

      问题 1 的答案:

      将特定应用的静态文件放在应用的文件夹中,而不是项目的“静态”文件夹中。

      /project/
          /polls/
              /static/
                  /polls/
                      foobar.css
                      foobar.js
              /templates/
                  /polls/
                      vote.html
          /static/
              specific-to-the-project.js
          /templates/
              specific-to-the-project.html
              base.html
      

      关键是您的应用文件夹中有一个static 文件夹,并且其中有一个与您的应用同名的文件夹。

      当您运行 collectstatic 时,它将收集在这些文件夹中找到的所有文件。

      注意,在引用文件时,您将执行类似...

      {% load staticfiles %}
      {% static 'app/foobar.css' %}
      

      问题 2 的答案:

      当您运行collectstatic 时,它会收集所有文件并将它们复制到一个由STATIC_ROOT 设置指定的中心位置。

      问题 3 的答案:

      不,因为您不处理 STATIC_ROOT 中的文件。您只需在原始位置处理文件,然后在准备好时运行collectstatic。

      问题 4 的答案:

      你可以做一个符号链接..

      不过,这确实是 st​​aticfiles 应用程序的用途。我建议对其进行改编并在此处阅读:https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/

      更新回复评论

      最佳做法是使用/<project_root>/polls/static/polls/foobar.css 而不是/<project_root>/polls/static/foobar.css。然后从您的模板通过{% static 'polls/foobar.css' %} 而不是{% static 'foobar.css' %} 访问foobar.css。请参阅下面的示例。

      示例

      在运行 collectstatic 之前

      /<project_root>
          /polls
              views.py
              urls.py
              ...
              /templates
                  vote.html
                  ...
              /static
                  /polls
                      foobar.css
                      foobar.js
          /otherapp
              views.py
              urls.py
              ...
              /templates
                  hello.html
                  ...
              /static
                  foobar.css
                  foobar.js
          /<static_root>
              (empty)
      

      运行 collectstatic 之后

      /<project_root>
          /polls
              views.py
              urls.py
              ...
              /templates
                  vote.html
                  ...
              /static
                  /polls
                      foobar.css
                      foobar.js
          /otherapp
              views.py
              urls.py
              ...
              /templates
                  hello.html
                  ...
              /static
                  foobar.css
                  foobar.js
          /<static_root>
              foobar.css
              foobar.js
              /polls
                  foobar.css
                  foobar.js
      

      每个应用中的静态文件夹的内容都复制到STATIC_ROOT目录中。

      将文件直接放在静态文件夹中而不是再添加一个目录并将它们放在那里,可能会导致文件名冲突。

      当您从事开源项目并且不知道应用程序将安装在哪里时,这一点更为重要。但这也被认为是最佳实践:)(我花了一段时间来适应它,但没有回头。)

      此外,只要 INSTALLED_APPS 中列出了“polls”,就无需将“polls/static/polls”添加到 STATICFILES_DIRS。

      【讨论】:

      • 它真的需要是应用程序/项目中该应用程序的静态内容吗?我们仍然需要将其添加到 STATICFILES_DIRS 对吗?
      • @Dineshkumar 查看我的更新答案。它解释了为什么将 /app/static/app/foobar.css 与 /app/static/foobar.css 视为最佳实践,并且您不需要向 STATICFILES_DIRS 添加任何内容。不过不要相信我的话,请阅读文档 :) docs.djangoproject.com/en/dev/ref/contrib/staticfiles
      • 精彩的回答!!解释了一切。
      猜你喜欢
      • 2014-01-26
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多