【发布时间】:2021-11-12 22:20:25
【问题描述】:
我正在尝试加载静态文件,但它显示以下错误:
GET http://127.0.0.1:8000/static/css/user/style.css net::ERR_ABORTED 404 (Not Found) - home:25
GET http://127.0.0.1:8000/static/css/user/style.css 404 (Not Found) - home:25
GET http://127.0.0.1:8000/static/img/logo.png 404 (Not Found) - home:149
GET http://127.0.0.1:8000/static/img/logo.png 404 (Not Found) - home:1
我的密码:
-管理员
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('Welcome.urls')),
path('auth/', include('Authentication.urls')),
path('ad/', include('Ads.urls')),
path('user/', include('UserDashboard.urls')),
path('admin/', include('AdminDashboard.urls')),
]
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
- 应用程序
模板
<link href="{% static 'css/user/style.css' %}" rel="stylesheet">
目录结构
代码说明
- 只需在根目录中添加静态文件并尝试在模板中导入它们,但未加载 css 文件
- 但是有些媒体文件像这个媒体
<link rel="shortcut icon" type="image/jpg" href="{% static 'img/logo.png' %}" />一样加载成功。 - 同样在 dir 结构中,我们可以看到 img 和 css 文件夹在 static 文件夹中的同一位置,并且从 image 文件夹中加载图像,但从 css 文件夹中加载 css。
【问题讨论】:
-
您记得在模板中{% load static %} 吗?
标签: python python-3.x django django-staticfiles django-static