[Django]-Authenticate() function not working django.contrib.auth

12👍

Try this:

def login_page(request):
    if request.method == "POST":
        username = request.POST['username']
        password = request.POST['password']
        seo_specialist = authenticate(username=username, password=password)
        if seo_specialist is not None:
            return HttpResponse("Signed in")
        else:
            return HttpResponse("Not signed in")
    else:
        # takes you to sign in form. 

Basically replace is_valid and cleaned_data with request.POST and then authenticate. Also make sure you have

from django.contrib.auth import authenticate

at the top of your views.

👤Paul

0👍

This is from django documentation. You seem to not have passed the request in …authenticate(request, user…)

This example shows how you might use both authenticate() and login():

    from django.contrib.auth import authenticate, login

    def my_view(request):
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(request, username=username, password=password)
        if user is not None:
            login(request, user)
            # Redirect to a success page.
            ...
        else:
            # Return an 'invalid login' error message.
            ...

Leave a comment