Django - CSS 文件不工作。收到错误 404。 "Failed to load resource: the server responded with a status of 404 (Not Found)"

Django - CSS file not working. Getting error 404 . "Failed to load resource: the server responded with a status of 404 (Not Found)"

我知道这个问题已经被问过很多次了,我也看过所有给出的答案,但是 none 其中对我有用。 我是新手,试图访问我的 Django 模板中的 CSS 文件,但它不起作用,我尝试了很多选项,但其中 none 对我有用。这里我在我的项目中使用 Django 2.2。你能帮我找出错误吗?

settings.py

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/

STATIC_URL = '/static/'

MEDIA_URL = '/images/'

STAICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]

MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images')

urls.py

from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include

urlpatterns = []

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('GalaxyOffset.urls')),
]

basic.css

body{
    background: url('{% static "../images/photo1.jpg" %}') no-repeat center center fixed;
    -webkit-background-size: cover;
    -moj-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
    background-color: #f5fbff;
    }

basic.html

<!doctype html>
<html lang="en">
<head>
    {% load static %}
    <link rel="stylesheet" type="text/css" href="{% static '/css/basic.css' %}">
    <title>{% block title%} {% endblock %} | Galaxy Offset</title>
</head>
<body>
<div class="container">
    <h1>Welcome to the first page </h1>
    {% block body %} {% endblock %}
</div>
</body>
</html>

项目层次结构 This is my Project hierarchy

在这里,我想访问我的 CSS 文件,但我不能。你能帮我弄清楚我在这里做错了什么或遗漏了什么吗?

您的代码在添加静态路由后重新定义了 urlpatterns。 urls.py 中的 urlpatterns 必须按以下顺序排列:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('GalaxyOffset.urls')),
]

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

和basic.css:

body{
background: url('{% static "images/photo1.jpg" %}') no-repeat center center fixed;
-webkit-background-size: cover;
-moj-background-size: cover;
-o-background-size: cover;
background-size: cover;
background-color: #f5fbff;
}