STATIC_ROOT returns wrong path

Hello,

I am not going to understand how “static” settings are working.

Here’s my settings:

BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

When I go to Python shell, this is the result:

>>> from django.conf import settings
>>> print(settings.BASE_DIR)
F:\test.com
>>> print(settings.STATIC_ROOT)
F:/static/
>>>

Why???

When you say you go to the Python shell, are you running python, or are you running manage.py shell (or shell_plus)?

Also you’re using Django 3.1’s pathlib-based BASE_DIR. It’s clearer then to use the pathlib API to define STATIC_ROOT: STATIC_ROOT = BASE_DIR / 'static'

See my post on this change: https://adamj.eu/tech/2020/03/16/use-pathlib-in-your-django-project/

python manage.py shell

This is the only part of this that doesn’t make sense to me. I can’t replicate this in my environment and I can’t think of any specific reason why it should be so.

If you’re looking to diagnose this more closely, I’d try something like adding the following to your settings file:
MY_FILE = __file__

Then, while in the shell, you can walk through those chained steps one by one to see what you’re getting after each call.
For example:

>>> settings.MY_FILE
>>> Path(settings.MY_FILE)
>>> Path(settings.MY_FILE).resolve()
>>> Path(settings.MY_FILE).resolve().parent
>>> Path(settings.MY_FILE).resolve().parent.parent

Base on @adamchainz reply, I changed the settings to this:

STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'static/' 

And now in the shell:

>>> from django.conf import settings
>>> print (settings.STATIC_ROOT)
F:\test.com\static
>>>

It is correct; however, when I put a test image file in "F:\test.com\static\images\test.jpg" and add it to the html like this:

{% load static %}
<img src="{% static 'images/test.jpg' %}">

I get 404 error. Why?

How are you running Django in this environment?

STATIC_ROOT is the destination for the collectstatic command - something done for your production environment.

When you’re working in your development environment, you’d put that image in the appropriate directory within your project. Then your development environment can use this file as well as it being available to collectstatic for production.

The Managing Static Files docs covers all the intricacies of static file management - admittedly, not a trivial topic.

Thanks. It’s a good thing not to have to import os anymore.

What I don’t understand is what {% load static %} template tag is actually loading. In my settings above, STATIC_ROOT is pointing to correct path but it doesn’t work. Why? I don’t understand the mechanism behind it.

See the load built-in template tag in addition to the previously identified links.

I know how to use load tag. I don’t know why STATIC_ROOT is not working in development. When I look at the source of output HTML file I see that the <img> tag in my template is rendered as "/static/images/test.jpg" which is absolutely correct, but it doesn’t work. Why? Django just ignores STATIC_ROOT in development?

Again, this is all covered in great detail in the docs for Managing static files, in this case the Serving static files in development section.

I’m actually not just dodging your questions - there are a lot of factors involved here regarding other settings in addition to STATIC_ROOT. This is not a trivial topic, and you should take some time reading the appropriate docs to understand everything that’s going on and the relationships between the various settings.

все дело в проектном (urls.py) там вся настройка и (# DEBUG = False
DEBUG = True)