首页 > 解决方案 > 如何修复styles.css:1 加载资源失败:服务器响应状态为404?

问题描述

我正在尝试学习 django 并且我的 css 文件无法与 html 文件 idk 链接为什么

这是静态文件夹的树:

├── images
├── js
├── plugins
└── styles
    └── styles.css

设置.py

STATIC_URL = '/static/'

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

索引.html

<!DOCTYPE html>
<html lang="en" dir="ltr">
    <head>
        <meta charset="utf-8">
        <title>Register</title>
        {% load static %}
        <link rel="stylesheet" href="{% static './styles/styles.css' %}">
    </head>
    <!-- <script src="../data/script-clac.js" type="text/javascript"></script> -->
    <body class="body1">
        <div class="div1">
            <h2>Register</h2>
            <p class="text">Name</p>
            <input type="text" placeholder="Enter your name" id="name">
            <p class="text">Username</p>
            <input type="text" placeholder="Choose a username" id="username">
            <p class="text">Password</p>
            <input type="password" placeholder="Choose a password" id="password">
            <p id="result"></p>
            <input type="checkbox" onclick="myFunction()">Show Password

            <button id="btn">Calculate</button>
        </div>
    </body>
</html>

错误信息:

styles.css:1 加载资源失败:服务器响应状态为 404(未找到)

很久以来我一直在尝试解决这个问题,请帮助我。

另外,这是我的项目目录

在此处输入图像描述

标签: pythondjangodjango-staticfiles

解决方案


在设置文件中添加STATICFILES_DIRS

前任:

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

推荐阅读