Skip to main content
 首页 » 编程设计

python之django 管理页面中没有 css

2025年04月02日52artech

我刚刚安装了 django 1.7,最近创建了新元素和应用程序。我创建了一个新的 super 用户。当我尝试登录到我的 super 用户帐户时,django 管理页面出现时没有 css。我不知道我做错了什么。谁能帮帮我?

这是我的设置.py:

 """ 
Django settings for blog project. 
 
For more information on this file, see 
https://docs.djangoproject.com/en/1.7/topics/settings/ 
 
For the full list of settings and their values, see 
https://docs.djangoproject.com/en/1.7/ref/settings/ 
""" 
 
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 
import os 
BASE_DIR = os.path.dirname(os.path.dirname(__file__)) 
 
 
# Quick-start development settings - unsuitable for production 
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/ 
 
# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = '%j$#fj6ddv$nkgv=v4kez6yi56&z$qffzr%gyz6b!ds-!5xi%e' 
 
# SECURITY WARNING: don't run with debug turned on in production! 
DEBUG = True 
 
TEMPLATE_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', 
    'post', 
) 
 
MIDDLEWARE_CLASSES = ( 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
) 
 
ROOT_URLCONF = 'blog.urls' 
 
WSGI_APPLICATION = 'blog.wsgi.application' 
 
 
# Database 
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases 
 
DATABASES = { 
    'default': { 
        'ENGINE': 'django.db.backends.sqlite3', 
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
    } 
} 
 
# Internationalization 
# https://docs.djangoproject.com/en/1.7/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/1.7/howto/static-files/ 
 
STATIC_URL = '/static/' 
STATIC_ROOT = os.path.join(BASE_DIR, 'static') 

请您参考如下方法:

您应该将以下内容添加到您的 settings.py

STATICFILES_DIRS = [ 
os.path.join(BASE_DIR, "static"), 
'C:/ProjectPath/static', 
] 

如果你只有 1 个 settings.py 文件,你应该改变 BASE_DIR 的值

BASE_DIR = os.path.dirname(os.path.abspath(__file__)) 

在 django 1.8 及更高版本中,您还应该定义一个“templates”文件夹,如“static”。 将您的 html 文件放入"template"文件夹,并在您的模板(html 文件)中添加 css/js 文件,如“static/css/bootstrap.css”。

TEMPLATES = [ 
{ 
    'BACKEND': 'django.template.backends.django.DjangoTemplates', 
    'DIRS': [os.path.dirname(__file__), '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', 
        ], 
    }, 
}, 
]