1

I am using django's admin site and in that css is not loadung so site is looking totally broken.In last 3-4 days i almost tried everything which is available around the web to fix it but its not getting fixed. As this is my new account i am not able to post the images. If anyone have any idea to solve this issue then please tell me!!

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.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure--yt4su$h2u!*nnnl=)_)7@0(z!63t2jvf#zb@+3sa^cc-514)!'

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

ALLOWED_HOSTS = []


# Application definition

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

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 = 'sample.urls'

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

WSGI_APPLICATION = 'sample.wsgi.application'


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

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


# Password validation
# https://docs.djangoproject.com/en/5.2/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.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


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

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

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

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

This is my setting.py file code.


from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns = [
    path('admin/', admin.site.urls),
]

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

This is my urls.py file code.
5
  • can you tell us browser console log ? Commented May 28 at 8:48
  • Its empty i just show Commented May 29 at 9:27
  • what did you tried so far exactly? Commented May 30 at 7:55
  • setting STATIC_URL and STATIC_ROOT in settings.py file. hard refreshing the page, so the cached files gets remove and so that the page without css being removed from cached files of browser. -Setting up whitenoise idea and all neccesity in it which also fails. -Reinstalled python,django everything total 3 times till now, but didn’t work. -Creating new project from sracth which also didn’t work. -Creating completely new virtual environment and setting up things in it and it also didnt worked. Commented May 31 at 9:28
  • -Creating a whole new user in my laptop so if there is misconfiguration in my laptop setting so it gets removed. As on creating new user(in laptop), all settings comes to default but it also didn’t work. -Changing browsers like edge,firefox,brave Commented May 31 at 9:28

2 Answers 2

0

I am not sure but have you tried running

python manage.py collectstatic

before initiating the server. As you have mentioned document_root=settings.STATIC_ROOT in urlpatterns, you must collect static files in staticfiles folder inside your parent project folder.

Sign up to request clarification or add additional context in comments.

3 Comments

I have tried it as i said i have tried almost everything which is available around the web. So yes i have tried that also but still not working
Can you replace static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) with this re_path(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}). Also add from django.urls import re_path and your STATIC ROOT should be STATIC_ROOT = BASE_DIR / 'static'
Its still same, tried this also
0

update you allowed_host

ALLOWED_HOSTS = ['*']

1 Comment

Tried it, didnt worked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.