I had created a new Django app, and I noticed it's admin page was loading without css. I set the STATIC_URL
and the STATIC_ROOT
in settings.py according to this solution, but it still does not work.
After some digging, I found this error when I open Chrome Dev Tools on Admin Page
[my_web_page] Refused to apply style from 'http://[my_web_page]/static/admin/css/base.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
[my_web_page] Refused to apply style from 'http://[my_web_page]/static/admin/css/responsive.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
[my_web_page] Refused to apply style from 'http://[my_web_page]/static/admin/css/login.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
[my_web_page] Refused to apply style from 'http://[my_web_page]/static/admin/css/responsive.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
[my_web_page]/:1 Refused to apply style from 'http://[my_web_page]/static/admin/css/login.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
[my_web_page]/:1 Refused to apply style from 'http://[my_web_page]/static/admin/css/base.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
I suppose that the app is finding my admin css, but it is refusing to apply the style.
I tried adding
import mimetypes
mimetypes.add_type("text/css", ".css", True)
to my settings.py as I had seen this solution somewhere, but to no avail.
Thank you for your time.
EDIT: here is my settings.py
"""
Django settings for mysite project.
Generated by 'django-admin startproject' using Django 3.0.8.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""
import os
import mimetypes
# A Bug is was encountering
mimetypes.add_type("text/css", ".css", True)
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '################################'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'students_app.apps.StudentsAppConfig',
'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 = 'mysite.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'mysite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/3.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/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
EDIT 2: I am using pythonanywhere's, maybe that has something to do?
If the Django admin page is rendered without style, you probably set DEBUG=False
in your settings.py.
With DEBUG=False
Django won't handle static files anymore. Check Serving the files box. here
Use debug=True in production isn't recommended.
An amazing way to handle static files is whitenoise
The problem was that when I was using Pythonanywhere, I wasn't using python manage.py runserver
command because my server was being hosted by pythonanywhere. I didn't know that with DEBUG=TRUE
, the runserver
command actually serves static files. What I ended up doing was setting my static_root
, static_url
, and staticfiles_dirs
to the correct values in settings.py
and then running python manage.py collectstatic
to compile all my static files into the right folder.
I realize that my answer needs elaboration, so I am describing what I ended up setting my settings to.
For my STATIC_ROOT
, I ended up setting it to BASE_DIR / "cdn_test" / "static"
. I did this because when you deploy your project, you will end up hosting your static files on a server(a cdn) that will host and serve your static files*. In production, you will also store your media (images) and more in your cdn folder.
For my STATIC_URL
, I just set it to /static/
. You can read more about it in the Django docs .
Finally, for my STATICFILES_DIRS
, I set it to
STATICFILES_DIRS = [
BASE_DIR / "static",
]`.
This is because Django copies the files from this static
folder into the cdn_test/static
folder. Whatever is in the static
folder is what you edit; the cdn_test
. If you have any other folders where you have static files, you can add them to your STATICFILES_DIRS
.
Now, whenever you make changes to your static
files, you will run python manage.py collectstatic
. This is not like development, where Django automatically serves your static files for you.
I hope all this makes sense because it gave me a fair bit of confusion initially when I was learning.
*Note that you have to create the cdn_test
and the static
dir inside it by yourself.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.