Python Django管理页面和CSS
我对django很陌生。我只是做了创建一个只有一个应用程序的项目的基本步骤。我使用python manage.py runserver启动了服务器,最后它给了我我的主页。但是当我打开管理页面时,它并没有像应该的那样使用正确的CSS。我经历了一些解决方案,比如添加以下代码,以便正确添加静态文件Python Django管理页面和CSS,python,django,Python,Django,我对django很陌生。我只是做了创建一个只有一个应用程序的项目的基本步骤。我使用python manage.py runserver启动了服务器,最后它给了我我的主页。但是当我打开管理页面时,它并没有像应该的那样使用正确的CSS。我经历了一些解决方案,比如添加以下代码,以便正确添加静态文件 STATIC_ROOT = os.path.join(BASE_DIR, 'static') STATIC_URL = '/static/' 但在运行python manage.py collectst
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
但在运行python manage.py collectstatic之后,这并没有反映任何情况。我还需要做些别的事情吗
这是settings.py文件
import os
# 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 = '(9szxe#x0q67)^wc6us01*i$=1)!&f7)530%73=pvzzhwp23gp'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'technicalCourse.apps.TechnicalcourseConfig',
'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 = 'website.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 = 'website.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/'
url.py
from django.urls import path,include
from django.contrib import admin
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('technicalCourse.urls')),
]
希望找到解决办法
提前感谢。这是我在settings.py中为静态文件编写路径的方式,它对我很有用
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR,'static'),]
我们把它写在一个列表里。。。您不需要对URL.py做任何操作,并且在html文件上(在html标记之前)不要忘记添加{%load static%}
{% load static %}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
</body>
</html>
{%load static%}
希望这有帮助这是我在settings.py中为静态文件编写路径的方式,它对我很有用
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR,'static'),]
我们把它写在一个列表里。。。您不需要对URL.py做任何操作,并且在html文件上(在html标记之前)不要忘记添加{%load static%}
{% load static %}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
</body>
</html>
{%load static%}
希望这是有帮助的是的,除了包含静态根以外,我没有做任何更改,它应该是wrk的,但它不是。你能包含你的URL吗?@schillingt include the URL.py。是的,除了包含静态根之外,我没有做任何更改,它应该是wrk,但不是。你能包括你的URL吗?@schillingt包括URL.py。