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.
-
can you tell us browser console log ?danial hadi– danial hadi2025年05月28日 08:48:35 +00:00Commented May 28, 2025 at 8:48
-
Its empty i just showRaj Jivani– Raj Jivani2025年05月29日 09:27:19 +00:00Commented May 29, 2025 at 9:27
-
what did you tried so far exactly?Dennis K– Dennis K2025年05月30日 07:55:50 +00:00Commented May 30, 2025 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.Raj Jivani– Raj Jivani2025年05月31日 09:28:31 +00:00Commented May 31, 2025 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,braveRaj Jivani– Raj Jivani2025年05月31日 09:28:57 +00:00Commented May 31, 2025 at 9:28
2 Answers 2
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.
3 Comments
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'update you allowed_host
ALLOWED_HOSTS = ['*']
1 Comment
Explore related questions
See similar questions with these tags.