尝试加载静态文件,但显示以下错误。
错误
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
代码
-行政
设置.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
网址.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 结构中,我们可以在 static 文件夹的同一位置看到 img 和 css 文件夹,并且加载了 image 文件夹中的图像,但 css 文件夹中的 css 没有。