Where to store static files in django app, How to include static files from multiple locations from nginx
I have a django app, static files are served with nginx. I want to include an application (function) in a project.
I was thinking about creating a newapp and putting the files in a static folder. (But I need to change the nginx conf to serve files from these dirs)
project
|
|-templates
|-static
|
|-js
|-css
newapp
|
|-static
|
|-js
|-css
-
which is the preferred way of storing static files, templates (in the app? (were the tests under this app that I felt clearer))
-
if we want to serve static files from two places, how to change nginx conf?
my current nginx conf is like
location /static/ { alias /myproject/location/static/; }
-
if we put all filse in this main project (static, templates), wouldn't it be difficult to maintain?
-
there is a way to match like
alias / myproject / * / static
source to share
Answer to question 1:
Place the application-specific static files in the application folder, not in the static project 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
The main thing is that you have a folder static
in your application folder and it has a folder with the same name as your application.
When you run it collectstatic
, it will collect all the files found in those folders.
Note that when accessing files, you will be doing something like ...
{% load staticfiles %}
{% static 'app/foobar.css' %}
Answer to question 2:
When you run it collectstatic
, it will collect all the files and copy them to one central location specified by the parameter STATIC_ROOT
.
Answer to question 3:
No, because you are not working with files located in STATIC_ROOT
. You only work with files in the original location and then launch collectstatic
when you're ready.
Answer to question 4:
You can make a sim link.
Indeed, this is what the staticfiles application is for. I suggest adapting it and reading about it here: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/
Update reply comment
Your best bet is to do this /<project_root>/polls/static/polls/foobar.css
instead /<project_root>/polls/static/foobar.css
. Then from your template access foobar.css via {% static 'polls/foobar.css' %}
instead {% static 'foobar.css' %}
. See example below.
Example
Before running 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 running 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 application are copied to the STATIC_ROOT directory.
You run the risk of file name clashes by putting the files directly into a static folder, adding another directory and placing them there.
This is more important when you are working on an open source project and do not know where the application will be installed. But this is also considered best practice :) (It took me a while to adapt it, but I haven't looked back.)
Also, you do not need to add polls / static / polls to STATICFILES_DIRS if your INSTALLED_APPS says polls.
source to share
This is why we have an staticfiles
app . Place the assets for each application inside this application, then execute in the deployment manage.py collectstatic
and they will all be collected in subdirectories under / static /
source to share