I am in the process of implementing user connection on my application, I have created superusers (MOUF1119 and WASS1111) they connect on the admin and application side, but simple users cannot connect. I do not know why. I share with you my code and a capture of my users
Project/comptes/models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
from .manager import *
from Entite1.models import Entite1
# Create your models here.
class CustomUser(AbstractUser):
username= None
email = models.EmailField()
profil=models.CharField(max_length=10,unique=True)
is_profil_verified= models.BooleanField(default=False)
Entite1= models.ForeignKey(Entite1, on_delete=models.CASCADE, related_name='entite',blank=True, null=True)
objects= UserManager()
USERNAME_FIELD = 'profil'
REQUIRED_FIELDS=[]
Project/comptes/manage.py
from django.contrib.auth.base_user import BaseUserManager
class UserManager(BaseUserManager):
use_in_migrations = True
def create_user(self,profil,password=None, **extra_fields):
if not profil:
raise ValueError('Le profil est manquant')
user = self.model(profil=profil,**extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self,profil,password=None, **extra_fields):
extra_fields.setdefault('is_staff',True)
extra_fields.setdefault('is_superuser',True)
extra_fields.setdefault('is_active',True)
return self.create_user(profil,password,**extra_fields)
Project/comptes/views.py
from django.shortcuts import render,redirect
from django.contrib.auth import authenticate, login,logout
from django.views.decorators.csrf import csrf_protect
# Create your views here.
@csrf_protect
def login_view(request):
if request.method == 'POST':
profil = request.POST['profil']
password = request.POST['password']
user = authenticate(request, profil=profil, password=password)
if user is not None:
login(request, user)
# Redirigez l'utilisateur vers la page souhaitée après la connexion
return redirect('ticket')
else:
# Gestion des erreurs de connexion
return render(request, 'comptes/login1.html', {'error_message': 'Nom d\'utilisateur ou mot de passe incorrect'})
# Si la méthode HTTP n'est pas POST, affichez simplement la page de connexion
return render(request, 'comptes/login1.html')
def logout_view(request):
logout(request)
return redirect('connexion')
login1.html
{% extends 'main.html' %}
{% block content %}
{% load static %}
<section class="h-100 gradient-form" style="background-color: #eee;">
<div class="container py-5 h-100">
<div class="row d-flex justify-content-center align-items-center h-100">
<div class="col-xl-10">
<div class="card rounded-3 text-black">
<div class="row g-0">
<div class="col-lg-6">
<div class="card-body p-md-5 mx-md-4">
<form method="post" action="{% url 'connexion' %}">
{% csrf_token %}
<div class="text-center">
<img src="{% static 'images/mkb_logo.png' %}" style="width: 195px;" alt="logo">
</div>
<p>Veuillez vous connecter à votre compte</p>
{% if error_messsage %}
<div class="alert alert-danger" role="alert">
{{error_messsage}}
</div>
{%endif%}
<div class="form-outline mb-4">
<input type="text" name="profil" class="form-control" placeholder="Profil" />
</div>
<div class="form-outline mb-4">
<input type="password" name="password" class="form-control" placeholder="Mot de passe" />
</div>
<div class="text-center pt-1 mb-5 pb-1">
<button class="btn btn-primary btn-block fa-lg gradient-custom-2 mb-3" type="submit"> <b>Connexion </b></button>
</div>
</form>
</div>
</div>
<div class="col-lg-6 d-flex align-items-center gradient-custom-2">
<img src="{% static 'images/log.jpg' %}" style="width: 385px;" alt="logo">
</div>
</div>
</div>
</div>
</div>
</div>
</section>
{% endblock %}