【发布时间】: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