css - 尽管正确配置了 css 文件,但出现静态 404 错误
问题描述
我加载了静态文件并为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 没有加载它显示的蚂蚁:
应用论坛的文件结构:
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
它会报告同样的错误。
我的代码中可能有什么问题?
解决方案
如果DEBUG=True
然后将 urls.py 添加到urlpatterns
urlpatterns = [....] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
对于具有前缀的静态 URL,STATICFILES_DIRS 可能如下
STATICFILES_DIRS = [
# ...
("forums", os.path.join(os.path.dirname(__file__), "../static/forums/")),
]
推荐阅读
- google-cloud-functions - 当我们在firebase功能+带有缓存的firebase托管上部署服务器端渲染(例如nuxt ssr)时如何解决版本不匹配问题
- sas - 数据步骤内的宏执行
- php - Content-Disposition 不接受变量
- angular - 我是否需要完成一个仅用于组成另一个 Observable 的主题?
- javascript - 同一脚本中不同点的同一变量的不同值 Vanilla Javascript
- .net - 将项目添加到元组列表时出现 VB.NET NullReferenceException
- php - 如何在 Laravel 中将此 SQL 查询转换为 eloquent
- python - 基于序列弱顺序的列表比较
- html - 如何在不触发屏幕阅读器的情况下聚焦元素
- vim - Vim - 如何使用 vim 浏览,离开和停留在浏览的位置