“Otentikasi Kustom Django” Kode Jawaban

Django mengotentikasi dengan email

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

Otentikasi Kustom 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

Otentikasi Kustom 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

Jawaban yang mirip dengan “Otentikasi Kustom Django”

Pertanyaan yang mirip dengan “Otentikasi Kustom Django”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya