无法使用 Django 中的普通用户帐户登录

问题描述

我正在开发具有个性化用户模型的应用。我可以使用超级用户登录,但是当我尝试使用普通用户时,出现密码和用户名错误的错误。
查看用户的数据,发现普通用户的密码没有经过hash。

模型定义models.py

from django.db import models
from django.contrib.gis.db import models
from phonenumber_field.modelfields import PhoneNumberField
from django.contrib.gis.db import models as gis_models
from django.contrib.auth.models import AbstractBaseUser,PermissionsMixin
from .managers import EmployeeManager


class Employee(AbstractBaseUser,PermissionsMixin):
    PERMANENT = 'Per.'
    TEMPORAIRE = 'Tem.'
    VIREMENT = 'Vir'
    ESPECES = 'Esp'
    TYPE_OF_CONTRACT = [
        (TEMPORAIRE,'Temporaire'),(PERMANENT,'Permanent'),]
    PAYMENT_MODE = [
        (VIREMENT,'Virement'),(ESPECES,'Especes'),]
    first_name = models.CharField(max_length=128,blank=False,null=False)
    last_name = models.CharField(max_length=128,null=False)
    registration_number = models.PositiveSmallIntegerField(unique=True,null=False)
    email = models.EmailField()
    driving_licence = models.PositiveIntegerField(blank=True,null=True)
    recruitment_date = models.DateField(auto_now=False,auto_now_add=False,blank=True,null=True)
    phone = PhoneNumberField(blank=True,help_text='numéro de telephone',null=True)
    is_exploitation_admin = models.BooleanField(default=False)
    is_supervisor = models.BooleanField(default=False)
    is_controlor = models.BooleanField(default=False)
    is_driver = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=True)

    USERNAME_FIELD = 'registration_number'
    REQUIRED_FIELDS = ['first_name','last_name','email']

    objects = EmployeeManager()

    def __str__(self):
        return (self.first_name)


class Supervisor(Employee):
    zone_name = models.CharField(max_length=128,blank=True)
    def __str__(self):
        return (self.last_name)

登录的模板是:

{% extends 'Drivers_App_Management/base.html' %}
{% block content %}
<!DOCTYPE html>
<html>

<head>
    <title>Login</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.1/css/all.css" integrity="sha384-gfdkjb5BdAXd+lj+gudLWI+BXq4IuLW5IT+brZEZsLFm++aCMlF1V92rMkPaX4PP" crossorigin="anonymous">
</head>
<body>
    {% if form.errors %} <p>votre numero et mot de pass sont incorrects</p> {% endif %}
    <div class="container h-100">
        <div class="d-flex justify-content-center h-100">
            <div class="user_card">
                <div class="d-flex justify-content-center">
                    <h3 id="form-title">LOGIN</h3>
                </div>
                <div class="d-flex justify-content-center form_container">
                    <form method="POST" action="{% url 'login' %}">
            {% csrf_token %}
                        <div class="input-group mb-3">
                            <div class="input-group-append">
                                <span class="input-group-text"><i class="fas fa-user"></i></span>
                            </div>
                            {{form.username.label}}
                            {{form.username}}
                        </div>
                        <div class="input-group mb-2">
                            <div class="input-group-append">
                                <span class="input-group-text"><i class="fas fa-key"></i></span>
                            </div>
              {{form.password.label}}
                            {{form.password}}
                        </div>
                        <div class="d-flex justify-content-center mt-3 login_container">
                                <input class="btn btn-primary" type="submit" value="Login">
                                <input type="hidden" name="next" value=" {{ next }} ">
                            </div>
                    </form>
                </div>
                <div class="mt-4">
                    <div class="d-flex justify-content-center links">
                        Don't have an account? <a href="{% url 'Drivers_App_Management:index' %}" class="ml-2">Sign Up</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>

</html>

{% endblock %}

更多信息
没有其他错误,并且在服务器中它没有显示任何错误(如您所见):

System check identified 1 issue (0 silenced).
December 24,2020 - 05:53:16
Django version 3.1.3,using settings 'Transport_Project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[24/Dec/2020 05:54:44] "GET /admin/Drivers_App_Management/driver/add/ HTTP/1.1" 200 39255
[24/Dec/2020 05:54:44] "GET /admin/jsi18n/ HTTP/1.1" 200 3187
[24/Dec/2020 05:57:38] "POST /admin/Drivers_App_Management/driver/add/ HTTP/1.1" 302 0
[24/Dec/2020 05:57:38] "GET /admin/Drivers_App_Management/driver/ HTTP/1.1" 200 34796
[24/Dec/2020 05:57:38] "GET /admin/jsi18n/ HTTP/1.1" 200 3187
[24/Dec/2020 05:58:00] "GET /admin/Drivers_App_Management/employee/add/ HTTP/1.1" 200 15864
[24/Dec/2020 05:58:00] "GET /admin/jsi18n/ HTTP/1.1" 200 3187
[24/Dec/2020 05:59:13] "POST /admin/Drivers_App_Management/employee/add/ HTTP/1.1" 200 16022
[24/Dec/2020 05:59:14] "GET /admin/jsi18n/ HTTP/1.1" 200 3187
[24/Dec/2020 05:59:51] "POST /admin/Drivers_App_Management/employee/add/ HTTP/1.1" 200 16022
[24/Dec/2020 05:59:51] "GET /admin/jsi18n/ HTTP/1.1" 200 3187
[24/Dec/2020 17:07:27] "POST /admin/Drivers_App_Management/employee/add/ HTTP/1.1" 200 16022
[24/Dec/2020 17:07:31] "GET /admin/jsi18n/ HTTP/1.1" 200 3187
[24/Dec/2020 17:08:14] "GET /admin/Drivers_App_Management/controlor/ HTTP/1.1" 200 11909
[24/Dec/2020 17:08:14] "GET /admin/jsi18n/ HTTP/1.1" 200 3187
[24/Dec/2020 17:08:22] "GET /admin/Drivers_App_Management/controlor/274/change/ HTTP/1.1" 200 43329
[24/Dec/2020 17:08:22] "GET /admin/jsi18n/ HTTP/1.1" 200 3187
[24/Dec/2020 17:08:43] "GET /accounts/login/?next=/Drivers_App_Management/cities HTTP/1.1" 200 3533
[24/Dec/2020 17:09:14] "POST /accounts/login/ HTTP/1.1" 200 3599
[24/Dec/2020 17:09:29] "GET /accounts/login/?next=/Drivers_App_Management/gazstations HTTP/1.1" 200 3538
[24/Dec/2020 17:09:49] "GET /Drivers_App_Management/ HTTP/1.1" 200 189
[24/Dec/2020 17:09:52] "GET /accounts/login/?next=/Drivers_App_Management/ HTTP/1.1" 200 3527
[24/Dec/2020 17:10:26] "POST /accounts/login/ HTTP/1.1" 200 3593

更新 ma​​nagers.py 中的用户创建代码:

from django.contrib.auth.base_user import BaseUserManager
from django.utils.translation import ugettext_lazy as _


class EmployeeManager(BaseUserManager):
    def create_user(self,email,first_name,last_name,password,registration_number,**extra_fields):
        if not last_name:
            raise ValueError(_('Le nom est obligatoire'))
        if not first_name:
            raise ValueError(_('Le prenom est obligatoire'))
        user = self.model(email=email,registration_number=registration_number,first_name=first_name,last_name=last_name,password=password,is_staff=is_staff,is_driver=is_driver,is_active=is_active,is_controlor=is_controlor,is_supervisor=is_supervisor,is_exploitation_admin=is_exploitation_admin)
        user.set_password(password)
        user.save()
        return user

    def create_superuser(self,password1,password2,**extra_fields):
        extra_fields.setdefault('is_exploitation_admin',True)
        extra_fields.setdefault('is_supervisor',True)
        extra_fields.setdefault('is_controlor',True)
        extra_fields.setdefault('is_driver',True)
        extra_fields.setdefault('is_superuser',True)
        extra_fields.setdefault('is_staff',True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError(_('Superuser must have is_staff=True.'))
        if extra_fields.get('is_superuser') is not True:
            raise ValueError(_('Superuser must have is_superuser=True.'))
        return self.create_user(email,**extra_fields)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...