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
which is preferred way to keep the static files, templates (in the app? (had tests under that app i felt is more clear))
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/; }
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
alias /myproject/*/static
Answer to question 1:
Put the static files for the particular app in the app's folder, not the project's 'static' folder.
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...
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 theSTATIC_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 runcollectstatic
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
After you run collectstatic
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.
That is exactly why we have the
staticfiles
app. Put the assets for each app inside that app, then on deployment runmanage.py collectstatic
and they will all be collected in subdirectories under /static/