【问题标题】:Django Admin loading without css没有css的Django Admin加载
【发布时间】:2020-11-16 18:17:23
【问题描述】:

我创建了一个新的 Django 应用程序,我注意到它的管理页面在没有 css 的情况下加载。我根据this解决方案在settings.py中设置了STATIC_URLSTATIC_ROOT,但是还是不行。

经过一番挖掘,我在管理页面上打开 Chrome 开发工具时发现了这个错误

[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. 

我想该应用正在寻找我的管理员 css,但它拒绝应用该样式。

我尝试添加

import mimetypes

mimetypes.add_type("text/css", ".css", True)

到我的 settings.py,因为我在某处看到过这个解决方案,但无济于事。

感谢您的宝贵时间。

编辑:这是我的 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/'

编辑 2:我正在使用 pythonanywhere,也许这有关系?

【问题讨论】:

  • 不足以解决问题。请分享您的设置。py
  • 谢谢你告诉我!已添加

标签: python css django


【解决方案1】:

如果 Django 管理页面没有样式呈现,您可能在 settings.py 中设置了DEBUG=False

使用DEBUG=False Django 将不再处理静态文件。选中提供文件框。 here

不建议在生产中使用 debug=True。

处理静态文件的一个绝妙方法是whitenoise

【讨论】:

  • 不,我的 DEBUG 变量设置为 True。将检查白噪声,但我认为 django 管理员正在查找文件,但拒绝应用样式。谢谢
  • 如果您不在生产环境中,这应该可以工作
  • 我分享了我的settings.py。请立即查看
  • 你试过python3 manage.py collectstatic命令吗?
  • 是的,我做到了。它将我的静态文件放在应用根目录下的static 目录中
【解决方案2】:

问题是,当我使用 Pythonanywhere 时,我没有使用 python manage.py runserver 命令,因为我的服务器由 pythonanywhere 托管。我不知道使用DEBUG=TRUErunserver 命令实际上服务于静态文件。我最终做的是将我的static_rootstatic_urlstaticfiles_dirs 设置为settings.py 中的正确值,然后运行python manage.py collectstatic 将我所有的静态文件编译到正确的文件夹中。

编辑

我意识到我的答案需要详细说明,所以我正在描述我最终设置的设置。

对于我的STATIC_ROOT,我最终将其设置为BASE_DIR / "cdn_test" / "static"。我这样做是因为当您部署项目时,您最终会将静态文件托管在将托管和提供静态文件的服务器(CDN)上*。在生产中,您还将媒体(图像)等存储在您的 cdn 文件夹中。

对于我的STATIC_URL,我只是将其设置为/static/。您可以在 Django docs 中阅读有关它的更多信息。

最后,对于我的STATICFILES_DIRS,我将其设置为

STATICFILES_DIRS = [
  BASE_DIR / "static",
]`. 

这是因为 Django 将文件从 static 文件夹复制到 cdn_test/static 文件夹中。 static 文件夹中的任何内容都是您编辑的内容; cdn_test。如果您有任何其他包含静态文件的文件夹,您可以将它们添加到您的STATICFILES_DIRS

现在,每当您对 static 文件进行更改时,您都将运行 python manage.py collectstatic。这不像开发,Django 会自动为您提供静态文件。

我希望所有这些都是有道理的,因为在我最初学习的时候它给了我相当多的困惑。

*请注意,您必须自己在其中创建cdn_teststatic 目录。

【讨论】:

  • 您好,您能分享一下您是如何在项目/应用文件夹中设置 static_root、static_url 相关的吗?
  • @AlexT 抱歉回复晚了,我已经解释了我在设置中修改的内容。自从我从事这个特定​​项目以来已经有一段时间了,我忘记了一些事情:)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
  • 2021-09-07
  • 2020-06-15
  • 1970-01-01
  • 2020-03-09
相关资源
最近更新 更多