Login don't work in django user not login

login dont work i don't where the problem please help.

https://youtu.be/tUqUdu0Sjyc ----- Dennis Ivy -- video learn in youtube

views

from django.contrib.auth import authenticate, login, logout

def login(request):
    if request.method == 'post':
        username = request.POST.get('username')
        password = request.POST.get('password')

        user = authenticate(request, username=username, password=password)
        if user is not None:
            login(request, user)
            return redirect('/')
    return render(request, 'login.html')

html

<form method="post">
    {% csrf_token %}
    <input type="username" name="username">
    <input type="password" name="password">
    <input type="submit" name="submit">
</form>

url

path('login/', views.login, name="login")

There are two problems here:

  • the request.method is always in uppercase; and
  • you have a name clash between the view function, and the function of auth.

You thus check this with:

from django.contrib.auth import authenticate
from django.contrib.auth import login as login_auth
from django.contrib.auth import logout


def login(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')

        user = authenticate(request, username=username, password=password)
        if user is not None:
            login_auth(request, user)
            return redirect('/')
    return render(request, 'login.html')
Back to Top