我是 Django 新手。这是我第一次尝试在 Django 中建立一个项目。我之前运行过这个应用程序,所有静态文件都按预期显示。但一段时间后重新启动我的机器,静态文件不再显示。
这是项目结构
这是我收到的错误消息
我的settings.py如下:
"""
Django settings for DebbyrichCollections project.
Generated by 'django-admin startproject' using Django 5.1.3.
For more information on this file, see
https://docs.djangoproject.com/en/5.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.1/ref/settings/
"""
from pathlib import Path
from django.conf.urls.static import static
import os
# 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.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-%0!($(z=yfs)tik4630krj64)m+h7o12@bujz)v+lh5j+3z%7='
# 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 = 'DebbyrichCollections.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'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',
],
},
},
]
WSGI_APPLICATION = 'DebbyrichCollections.wsgi.application'
# Database
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/5.1/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',
},
]
from django.conf import settings
# Internationalization
# https://docs.djangoproject.com/en/5.1/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.1/howto/static-files/
STATIC_URL = 'static/'
STATICFILEDIRS = [
os.path.join(BASE_DIR,STATIC_URL)
] + static(settings.STATIC_URL, document_root=os.path.join(settings.BASE_DIR, 'static'))
# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
static(…)
处理程序[Django-doc]:这是一个定义 URL 路径的工具,然后调用视图来提供静态文件,所以它确实 不属于settings.py
而是属于root urls.py
(或者另一个,虽然这很奇怪),所以 DebbyrichCollections/urls.py
看起来像:
# DebbyrichCollections/urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# …
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
在设置中,请不要将
STATIC_URL
与BASE_DIR
加入,这样没有多大意义,如果以后更改STATIC_URL
,会带来很多麻烦。
合作:
# settings.py
# …
STATICFILEDIRS = [os.path.join(BASE_DIR, 'static/')]
注意:Python 模块通常以 snake_case 编写,而不是 PascalCase,所以它 应该是
,而不是debbyrich_collections
。DebbyrichCollections