无法加载 JS 静态文件,但 style.css 文件仍然有效

Can't load JS static file, but style.css file still work

这是我的错误。 styles.css 和 index.js 文件在同一文件夹名称网络中。但是 styles.css 文件有效,而 index.js 文件无效,我的另一个项目是 Mail

[20/Feb/2022 14:17:59] "GET / HTTP/1.1" 200 1804
[20/Feb/2022 14:17:59] "GET /static/network/styles.css HTTP/1.1" 304 0
[20/Feb/2022 14:18:01] "GET /static/mail/inbox.js HTTP/1.1" 404 1667
[20/Feb/2022 14:18:01] "GET / HTTP/1.1" 200 1804

setting.py

STATIC_URL = '/static/'

index.html

{% extends "network/layout.html" %}
{% load static %}

{% block body %}
{% endblock %}

{% block script %}
    <script src="{% static 'network/index.js' %}" defer></script>
{% endblock %}

index.js

console.log('OK');

layout.html

{% load static %}

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>{% block title %}Social Network{% endblock %}</title>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
        <link href="{% static 'network/styles.css' %}" rel="stylesheet">
    </head>
    <body>
        <div class="body">
            {% block body %}
            {% endblock %}
        </div>
    </body>
</html>

向脚本添加类型并尝试:

<script type="text/javascript" src="{% static "network/index.js" %}"></script>

您应该在 layout.html

中添加 block script
{% load static %}

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>{% block title %}Social Network{% endblock %}</title>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
        <link href="{% static 'network/styles.css' %}" rel="stylesheet">
    </head>
    <body>
        <div class="body">
            {% block body %}
            {% endblock %}
        </div>

      {% block script %}
      {% endblock %}

    </body>
</html>