Can't login with normal user account in django

断了今生、忘了曾经 提交于 2021-01-07 02:43:14

问题


I'm working on an app where I have a personalized User model. I can use the superuser to login, but when I try with a normal user I get an error of wrong password and user name.
When I look at the data of the user, I find that the password of normal users isn't hashed.

The models definition 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, blank=False, null=False)
    registration_number = models.PositiveSmallIntegerField(unique=True, blank=False, 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)

The template of the login is:

{% 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 %}

More information
There no other errors, and in the server it show no errors (as you can see here):

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

update the user creation code in managers.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, first_name, last_name, registration_number, email, 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, first_name, last_name, password, registration_number, **extra_fields)

来源:https://stackoverflow.com/questions/65442569/cant-login-with-normal-user-account-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!