如何让静态文件在 Django 中工作 |网站图标未加载,但在进入该目录时出现

问题描述 投票:0回答:1

我在尝试让静态文件正常工作时遇到了麻烦,我已经尝试了很长一段时间不同的答案,并且我很困惑问题出在哪里。我试图在关闭调试的情况下加载我的CSS,但我认为CSS不出现是正常的,因为django不会在关闭调试的情况下加载静态文件。我的问题是当尝试将网站图标放入我的测试网站时。我还觉得以后会出现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>

urls.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'

错误:
“获取/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 帖子,没有什么真正帮助我,只是让我更加困惑。

非常感谢您的帮助,谢谢!

python html django favicon django-staticfiles
1个回答
0
投票

删除模板中的一个

}
{% static "favicon.ico" %}}
。您错误地在末尾添加了两个大括号。您可以看到,因为网址后面有一个额外/错误的
%7D

© www.soinside.com 2019 - 2024. All rights reserved.