Here’s my settings.py:
STATIC_ROOT = BASE_DIR / 'static'
STATIC_URL = 'static/'
DEBUG = TRUE
My application has a static folder with a furbar subfolder, also the templates have the correct references.
For example for the error "GET /static/furbar/assets/images/blog/blog-01.jpg HTTP/1.1" 404 1870
:
The address specified in the template:
{% static 'furbar/assets/images/blog/blog-01.jpg
.
The file is also present at the path STATIC_ROOT/furbar/assets/images/blog/blog-01.jpg
’’
I also ran collectstatics
, what could be the problem?
When using runserver
and DEBUG = True
, neither collectstatic
nor STATIC_ROOT
have anything to do with this. They’re basically unused in this situation.
If you’re not using runserver
, please describe how you’re running your project.
Please show what directory the blog-01.jpg is actually in, along with your STATICFILES_DIRS
and STATICFILES_FINDERS
settings.
I run it through runserver. I don’t quite understand you, why is it irrelevant?
as I understand from the documentation, after starting collectstatics, static_root collects statics from applications in particular, if default settings are set, and then (if no other server is used and DEBUG=True) django distributes statics from the folder specified in static_root, isn’t that right?
STATICFILES_DIRS and STATICFILES_FINDERS settings are default. the folder specified in static_root is in the same directory as manage.py (I checked all paths a hundred times). i don’t understand why some of the static is loaded and the rest is not.
I also want to note that I don’t quite understand the definition of STATIC_URL: is this the path that will be represented in the templates in {%static%}?
Because runserver
uses its own mechanisms for locating static files - particularly the STATICFILES_FINDERS
.
See the docs at How to manage static files (e.g. images, JavaScript, CSS) | Django documentation | Django, and pay particular attention to the differences between serving static files in development compared to deployment. You’ll notice that STATIC_ROOT
and collectstatic
are only referenced in the Deployment section and not in the development section.
So again, STATIC_ROOT
is not relevent in the development environment, and neither is collectstatic
.
If only some static files are not being displayed, then you either need to figure out what those files have in common - for example, are they all in the same directory? Or, you need to supply the information we’re asking for - otherwise, we can’t help you.
In this case, it means posting the requested settings from your project along with providing the actual directory names where these static files are located.
Actually you do - its precisely for that purpose:
1 Like
The problem turned out to be that the directories in static_root (in it the paths are correct) and the static directory in the application do not match each other, and I thought that the built-in static service server looks for static in static_root, but in fact, as you said, it looks for static in other places, in particular in the static directory in the application and it was this directory that did not match the url requests.
I’d like to summarize the results:
The built-in static server in django searches for static in applications in particular, and distributes static on the path specified in static_url, am I correct?
Not quite accurate. The built-in static server searches for static files based upon what directories are searched for by the STATICFILES_FINDERS
and any settings that those finders use. There is nothing “automatic” or “default” - it works strictly on what has been defined in the settings.
The application static directories are searched because of the use of the AppDirectoriesFinder
. Directories specified in STATICFILES_DIRS
are searched because of the FileSystemFinder
.
Here I am not quite sure what you mean by this.
When you have something in a template like {% static 'dir/file.png' %}
, a path is constructed by effectively concatenating STATIC_URL
to the parameter. So, if STATIC_URL='/static/'
, then the url rendered in the template is /static/dir/file.png
.
This is a completely separate and indepenent operation than the retrieval of that url by the static server. This operates no differently than if you had just defined the url as /static/dir/file.png
without using the static
tag.
When runserver receives a request with a url that matches /static/
at the beginning, regardless of how that url was defined in the template, and DEBUG=True
, then it will pass the rest of the url to the finders to try and locate the file. If a file matching that full path is found in one of the directories being searched, it will be returned to the browser.
1 Like