在django应用程序中保留静态文件的位置,如何从nginx从多个位置服务静态文件 [英] where to keep static files in django application, How to server static files from more than one location from nginx

查看:103
本文介绍了在django应用程序中保留静态文件的位置,如何从nginx从多个位置服务静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've a django application, static files are served using nginx. I want to include app (feature) in project.

我想到创建newapp并将文件放在静态文件夹下。 (但是我需要更改nginx conf来提供这些文件)

I thought of creating newapp and putting files under static folder. (But i need to change nginx conf to serve the files from those dirs)

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


 newapp
 |
 |-static
    |
    |-js
    |-css




  1. 这是保持静态文件的首选方法,模板(在应用程序中?(我觉得该应用程序下的测试更清晰))

  1. which is preferred way to keep the static files, templates (in the app? (had tests under that app i felt is more clear))

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

if we want to serve static files from two location how to change nginx conf?

我当前的nginx conf就像

my current nginx conf is like

location /static/ {  alias /myproject/location/static/; }




  1. 如果我们把所有的在主要项目(静态,模板)中,维护不会变得复杂吗?

  1. if we put all filse in that main project (static, templates) won't it become complex to maintain?

是否有一种匹配方式,如

is there a way to match like

别名/ myproject / * / static

alias /myproject/*/static


推荐答案

p> 回答问题1:

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

Put the static files for the particular app in the app's folder, not the project's 'static' folder.

/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 文件夹有一个文件夹与您的应用程序具有相同的名称。

Key thing being that you have a static folder in your app's folder and it has a folder in it with the same name as your app.

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

When you run collectstatic it will collect all of the files found in these folders.

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

Note, when referencing the files you will do something like...

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

回答问题2:

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

When you run collectstatic it will gather all of the files and copy them over to one central location which is specified by the STATIC_ROOT setting.

对问题3的回答:

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

No, because you don't work on the files that are in the STATIC_ROOT. You only work on the files in their original location, then run collectstatic when you are ready.

回答问题4:

你可以做一个sym链接..

You could do a sym link..

这就是staticfiles应用程序的用途。我建议您在此处进行修改并阅读: https://docs.djangoproject。 com / en / dev / ref / contrib / staticfiles /

Really though, that is what the staticfiles app is for. I suggest adapting it and reading about it here: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/

更新回覆评论

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

Best practice is to do this /<project_root>/polls/static/polls/foobar.css instead of /<project_root>/polls/static/foobar.css. Then from your template access foobar.css via {% static 'polls/foobar.css' %} instead of {% static 'foobar.css' %}. See the example below.

示例

/<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



After you run 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目录中。

The contents of the static folder in each app are copied into the STATIC_ROOT directory.

通过放置文件冲突风险您的文件直接在静态文件夹中,而不是添加一个目录并将其放在那里。

You risk filename collisions by putting your files directly in the static folder vs adding one more directory and putting them in there.

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

This is more important when you are working on an open source project and don't know where the app is going to be installed. But it is also considered best practice :) (It took me a while to adapt it but haven't looked back.)

另外,你不需要添加'只要在INSTALLED_APPS中列出民意调查,投票/静态/投票到STATICFILES_DIRS。

Also, you do not need to add 'polls/static/polls' to STATICFILES_DIRS as long as 'polls' is listed in your INSTALLED_APPS.

这篇关于在django应用程序中保留静态文件的位置,如何从nginx从多个位置服务静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆