【问题标题】:could not find js files in Django project在 Django 项目中找不到 js 文件
【发布时间】:2021-02-14 19:24:12
【问题描述】:

我在 windows 10 中使用 python 3.7 创建了一个 Django 项目。 设置.py:

    STATIC_URL = '/static/'
LOGIN_URL = '/login'
LOGIN_REDIRECT_URL = '/'

我使用超级用户创建了一些普通用户。但是普通用户账号无法登录网站,在power shell中出现如下错误:

[2020 年 11 月 2 日 13:33:03]“POST /login/?next=/HTTP/1.1”200 14278 不是 找到:/login/vendor/fontawesome-free/css/all.min.css [02/Nov/2020 13:33:04]“获取/login/vendor/fontawesome-free/css/all.min.css HTTP/1.1" 404 19761 未找到:/login/vendor/jquery/jquery.min.js [2020 年 11 月 2 日 13:33:04]“获取 /login/vendor/jquery/jquery.min.js HTTP/1.1" 404 19713 未找到: /login/vendor/bootstrap/js/bootstrap.bundle.min.js [2020 年 11 月 2 日 13:33:04]“获取/login/vendor/bootstrap/js/bootstrap.bundle.min.js HTTP/1.1" 404 19777 [2020 年 11 月 2 日 13:33:19] "POST /login/?next=/ HTTP/1.1" 200 14278 未找到: /login/vendor/bootstrap/js/bootstrap.bundle.min.js [2020 年 11 月 2 日 13:33:19]“获取/login/vendor/bootstrap/js/bootstrap.bundle.min.js HTTP/1.1" 404 19777 未找到:/login/vendor/jquery/jquery.min.js 未找到 找到:/login/vendor/fontawesome-free/css/all.min.css [02/Nov/2020 13:33:19]“获取/login/vendor/fontawesome-free/css/all.min.css HTTP/1.1" 404 19761 [2020 年 11 月 2 日 13:33:19]"获取 /login/vendor/jquery/jquery.min.js HTTP/1.1" 404 19713 未找到: /login/vendor/jquery/jquery.min.js [2020 年 11 月 2 日 13:33:19]“获取 /login/vendor/jquery/jquery.min.js HTTP/1.1" 404 19712 未找到: /login/vendor/bootstrap/js/bootstrap.bundle.min.js [2020 年 11 月 2 日 13:33:19]“获取/login/vendor/bootstrap/js/bootstrap.bundle.min.js HTTP/1.1" 404 19776

但我检查过我的静态文件夹中确实有他们的文件:

我尝试运行 python manage.py collect static 但出现错误:

    raise ImproperlyConfigured("You're using the static files app "
django.core.exceptions.ImproperlyConfigured: You're using the static files app without having set the STATIC_ROOT setting to a

文件系统路径。

enter image description here enter image description here

登录.html {% 加载静态 %}

<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>{% block title %}Login{% endblock %}</title>

    <!-- Custom fonts for this template-->
    <link href="{% static 'vendor/bootstrap/css/bootstrap.css' %}" rel="stylesheet" type="text/css">
    <link href="{% static 'vendor/fontawesome-free/css/all.min.css' %}" rel="stylesheet" type="text/css"/>
    <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i"
          rel="stylesheet">

    <!-- Custom styles for this template-->
    <link rel="stylesheet" type="text/css" href="{% static 'css/sb-admin-2.css' %}"/>

</head>

<body class="bg-gradient-primary">

<div class="container">

    <!-- Outer Row -->
    <div class="row justify-content-center">

        <div class="col-12">

            <div class="card o-hidden border-0 shadow-lg my-5">
                <div class="card-body p-0">
                    <!-- Nested Row within Card Body -->
                    <div class="row">
                        <div class="col-lg-8 d-none d-lg-block bg-login-image"></div>
                        <div class="col-lg-4">
                            <div class="p-5">
                                <div class="text-center pb-5">
                                    <img src="{% static 'image/logo.png' %}" height="60px">
                                </div>
                                <div class="text-center">
                                    <h1 class="h4 text-gray-900 mb-4">Welcome Back!</h1>
                                </div>
                                <form class="user" method="post">
                                    {% csrf_token %}
                                    <div class="form-group">
                                        <input class="form-control form-control-user" name="username" aria-describedby="emailHelp"
                                               placeholder="Username">
                                    </div>
                                    <div class="form-group">
                                        <input type="password" class="form-control form-control-user" name="password"
                                               placeholder="Password">
                                    </div>
                                    <div class="form-group">
                                        <div class="custom-control custom-checkbox small">
                                            <input type="checkbox" class="custom-control-input" id="customCheck">
                                            <label class="custom-control-label" for="customCheck">Remember Me</label>
                                        </div>
                                    </div>
                                    <button type="submit" class="btn btn-primary btn-user btn-block">
                                        Login
                                    </button>
                                </form>
                                <hr>
                                <div class="text-center">
                                    <a class="small" href="forgot-password.html">Forgot Password?</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>

    </div>

</div>

<!-- Bootstrap core JavaScript-->
<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script type="text/javascript"
        src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/js/bootstrap.min.js"></script>
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

<script src="{% static 'vendor/jquery-easing/jquery.easing.min.js' %}"></script>

<!-- Custom scripts for all pages-->
<script src="{% static 'js/sb-admin-2.min.js' %}"></script>

</body>

</html>

【问题讨论】:

  • 您是否在 urls.py 中启用了管理员。只需检查您的 URL 是否应该是 localhost:port/admin/login
  • 从您的图像来看,您的静态文件似乎不在默认或常规使用的文件夹 static 但如果您使用不同的静态文件文件夹,请确保在 STATICFILES_DIRS 设置中定义
  • @AshishGupta 我应该在哪里添加?
  • @RedgrenGrumbholdt 我有静态文件夹,但在控制台中它一直显示:/login/vendor/fontawesome-free/css/all.min.css HTTP/1.1" 404 19761 Not Found,所以我将供应商文件从静态文件夹复制到登录文件夹
  • 将登录文件夹中的文件复制到静态文件夹中。

标签: javascript python jquery django


【解决方案1】:

请按照步骤here 使静态文件正常工作。从您发布的错误来看,我猜以下配置错误:

  1. 设置中有奇怪的 STATIC_URL 值。看起来你有STATIC_URL='/login/'。通常它应该设置为STATIC_URL = '/static/'
  2. 确保将静态文件存储在您的某个应用程序中的static 文件夹中。 IE。如果您有一个名为bms 的应用程序,则将您的静态文件放入bms/static 文件夹。 Django 提供了有用的findstatic 命令来帮助你了解它在哪里寻找给定的静态文件,例如:python manage.py findstatic -v3 js/msp/table_header.js
  3. 请注意,仅当您使用 django 的开发服务器(runserver 命令)并且仅当您在设置中设置了DEBUG=True 时,django 才会自己提供静态文件。在生产环境中提供静态文件,请参阅doc

【讨论】:

  • 在我的setting.py中,STATIC_URL = '/static/' LOGIN_URL = '/login'
  • 我用的是django开发服务器,检查bms/static文件夹下确实有静态文件
  • 您可以尝试运行我在步骤 2 中描述的 findstatic 命令吗?您是否看到 django 正在查找静态文件的目录列表?
  • 另外,你没有提到 DEBUG 设置。您是否检查它是否设置为 True?
  • 调试在我的情况下一直设置为 Ture,因为我在调试过程中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
  • 2021-04-17
  • 1970-01-01
  • 1970-01-01
  • 2018-10-26
相关资源
最近更新 更多