Admin static files url

Hello!

  1. Under what settings will the link to static files in the admin panel begin
    admin/login/static/admin/css/base.css ?
    When I connect the dynaconf library, the static admin files stop working, it helps if I put / at the beginning of STATIC_URL.

STATIC_URL = 'static/'
STATIC_URL = '/static/'

  1. How can I see what settings dynaconf changes?
settings.py
"""
Django settings for stka project.

Generated by 'django-admin startproject' using Django 5.0.4.

For more information on this file, see
https://docs.djangoproject.com/en/5.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.0/ref/settings/
"""

from pathlib import Path


# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-j0ujmafi#n=erbe=db1x$a=%u-7r0a#x-+fi%18bm^jph0ejt6'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'main'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'stka.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            BASE_DIR / 'templates',
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'stka.wsgi.application'


# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/5.0/topics/i18n/

LANGUAGE_CODE = 'ru'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/

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

# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = ''
EMAIL_PORT = ''
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_SSL = True

DEFAULT_FROM_EMAIL = ''
NOTIFY_EMAIL = []


import dynaconf  # noqa
settings = dynaconf.DjangoDynaconf(__name__)  # noqa

# print(settings.DEBUG)

I’m not sure I understand what you’re trying to ask here, but as a general rule, your STATIC_URL setting should be an absolute URL. (In other words, /static/ is the correct setting.)

Likewise, in a deployment environment, your STATIC_ROOT setting should also be an absolute path, and ideally, outside the project dir.

  1. Why is STATIC_URL = “static/” relative by default?

  2. How the URL of a static file is formed.

  3. Under what settings can the URL of a static file look like this?
    http://127.0.0.1:8000/admin/static/admin/css/base.css

Automatically translated

Because I was wrong in my previous reply. I was looking at one of our projects, where we set it to that instead of looking at the default project template.

Typically, you use the {% static ... %} tag to define a reference to a static file. That’s where STATIC_URL is used. See the docs for STATIC_URL for more details, especially the “Note” box in that section.

There are three ways that I can think of off-hand:

  • STATIC_URL is set to admin/static/
  • There’s a reference to that file without using the static tag.
  • The SCRIPT_NAME environment variable is set to admin/

This situation happens if you use the dynaconf library, have you encountered it?
Apparently this library changes the default settings.

Sorry, never heard of it.

Side note: Please do not post images of code. When you’re looking to share code or other text here, copy / paste the code into the body of your post, surrounded between lines of three backtick - ` characters. (This means you’ll have a line of ```, then your code, then another line of ```.)

I looked through the code of the django/templatestag/static module but did not find a function where static links are generated. Could you please tell me where (in which module) static links are formed?

They’re defined in the templates. See the template files in django.contrib.admin.templates.admin

These files themselves contain links to static files, but how is the static URL formed?

I’m sorry, I’m not understanding what you’re trying to ask here. What is it that you’re trying to find out?

I want to find out why my static URLs in the admin panel become like this when using the dynaconf library.

http://127.0.0.1:8000/admin/static/admin/css/dark_mode.css

so I want to find the section of code where the static URL is generated and trace it

The URL in the HTML is generated by the static tag within the rendering process.

Some kind of magic, I did a trace, render returns the correct part of the URL, but I couldn’t find where h ttp://127.0.0.1:8000/admin/static/admin/css/base.css comes from.

    def render(self, context):
        url = self.url(context)
        if context.autoescape:
            url = conditional_escape(url)
        if self.varname is None:
            return url
        context[self.varname] = url
        return ""

watch
url : 'static/admin/css/base.css'