gpt4 book ai didi

css - 尽管正确配置了 css 文件,但出现静态 404 错误

转载 作者:太空宇宙 更新时间:2023-11-04 01:14:33 24 4
gpt4 key购买 nike

我为 bootstrap.min.css 加载了静态文件并精心配置了嵌套目录结构。然而,它意外地抛出了一个错误:

"GET /static/forums/bootstrap.min.css HTTP/1.1" 404 1685

index.html:

{% load static %}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Forum Home Page</title>
<link rel="stylesheet" href="{% static "forums/bootstrap.min.css" %}" />
</head>

css 未加载它显示:

enter image description here

应用论坛的文件结构:

forums
├── __init__.py
├── admin.py
├── apps.py
├── migrations
│   └── __init__.py
├── models.py
├── static
│   └── forums
│   └── bootstrap.min.css
├── templates
│   └── forums
│   └── index.html
├── tests.py
├── urls.py
└── views.py

和setting.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/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!

# 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 = 'forum.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'forums/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 = 'forum.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.11/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/1.11/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/1.11/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.11/howto/static-files/

STATIC_URL = '/static/'

STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"), #notice the comma
)

如果我注释掉 STATICFILES_DIRS,它会报告同样的错误。

我的代码中可能存在什么问题?

最佳答案

forums/bootstrap.min.css 文件位于 forums 应用的 static 目录中。

因此,您需要在 INSTALLED_APPS 中包含 'forums',以便应用程序目录静态文件查找器可以找到它。

当您将 os.path.join(BASE_DIR, "static") 添加到 STATICFILES_DIRS 时,Django 也会在您的元素No such file or directory 错误表明该目录不存在。要在运行 collectstatic 时停止错误,您可以创建目录或将其从 STATICFILES_DIRS 中删除。

关于css - 尽管正确配置了 css 文件,但出现静态 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50486127/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com