where to keep static files in django application,

2019-07-16 01:17发布

问题:

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

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. which is preferred way to keep the static files, templates (in the app? (had tests under that app i felt is more clear))

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

my current nginx conf is like

location /static/ {  alias /myproject/location/static/; }
  1. if we put all filse in that main project (static, templates) won't it become complex to maintain?

  2. is there a way to match like

    alias /myproject/*/static

回答1:

Answer to question 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

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.

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' %}

Answer to question 2:

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.

Answer to question 3:

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.

Answer to question 4:

You could do a sym link..

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/

Update answering comment

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.

Example

Before 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>
        (empty)

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

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.)

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



回答2:

That is exactly why we have the staticfiles app. Put the assets for each app inside that app, then on deployment run manage.py collectstatic and they will all be collected in subdirectories under /static/