“Authentification personnalisée de Django” Réponses codées

Django s'authentifie avec un e-mail

from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend

class EmailBackend(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        UserModel = get_user_model()
        try:
            user = UserModel.objects.get(email=username)
        except UserModel.DoesNotExist:
            return None
        else:
            if user.check_password(password):
                return user
        return None
Tony Chanel

Authentification personnalisée de Django

def getLogin(request):
    if request.user.is_authenticated:
        return redirect('index')
    else:
        if request.method == "POST":
            user = request.POST.get('user')
            password = request.POST.get('pass')
            auth = authenticate(request, username=user, password=password)
            if auth is not None:
                login(request, auth)
                return redirect('index')
        return render(request, "login.html")
Coding with Nasir

Authentification personnalisée de Django

<form action="" method="POST">
    <div class="form-group">
        <label for="email" class="active">Email address</label>
        <input type="email" name="email" class="form-control" id="email" placeholder="Enter email">
        <div class="email-mg"></div>
    </div>
    <div class="form-group">
        <label for="password" class="active">Password</label>
        <input type="password" name="password" class="form-control" id="password" placeholder="Password">
        <div class="pass-mg"></div>
    </div>
    <button type="submit" class="btn btn-primary btn-block waves-effect waves-light" id="loginBtn">Log in</button>
</form>
Coding with Nasir

Réponses similaires à “Authentification personnalisée de Django”

Questions similaires à “Authentification personnalisée de Django”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code