我有麻烦试图让我的静态文件工作,我已经尝试了很长一段时间不同的答案,我越来越困惑什么是问题。我试图在调试关闭的情况下加载css,但我认为css不出现是正常的,因为django在调试关闭的情况下不会加载静态文件。我的问题是当试图把一个favicon到我的测试网站。我也认为在未来我会有JS的并发症,等等。
main.html
{% load static %}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Hello, Django</title>
<!-- load staticfiles-->
<link rel="stylesheet" href="{% static 'main.css' %}">
<link rel="shortcut icon" href="{% static "favicon.ico" %}}" type="image/png">
</head>
<body>
</body>
</html>
url.py
from django.urls import path
from f1trackerapp import views
from django.contrib.staticfiles.storage import staticfiles_storage
from django.views.generic.base import RedirectView
urlpatterns = [
path("", views.f1trackerapp, name="f1trackerapp"),
path('favicon.ico', RedirectView.as_view(url=staticfiles_storage.url('favicon.ico')))
]
settings.py(stackoverflow不允许我发布完整的settings.py代码)
...
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
...
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['127.0.0.1']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'f1trackerapp',
]
...
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 = 'f1stats.wsgi.application'
# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/4.2/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/4.2/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/4.2/howto/static-files/
STATIC_URL = 'static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
错误:
- “GET /static/favicon.ico%7D HTTP/1.1”404 1911*
我的项目目录看起来像这样:
F1stats
.venv
.vscode
f1stats (project)
--project files--
f1trackerapp (app)
--app files--
templates
main.html
migrations
static
favicon.ico.png
main.css
db.sqlite3
manage.py
requirements.txt
我已经读了很多文档,和stackoverflow职位,没有什么真正帮助我只是让我更困惑。
帮助将不胜感激,谢谢!
1条答案
按热度按时间ggazkfy81#
从模板
{% static "favicon.ico" %}}
中删除一个}
。你在末尾错误地加了两个花括号。你可以看到,因为URL后面有一个额外的/错误的%7D
。