Python Forum

Full Version: Django authenticate problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
this is my form in form.py:
from django import forms
from django.contrib.auth.models import User


class UserRegistrationForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('username', 'email', 'password', 'first_name', 'last_name')


class UserLoginForm(forms.Form):
    username = forms.CharField()
    password = forms.CharField()
and this is my view for login:

def login_account(request):
    if request.method == 'POST':
        login_form = forms.UserLoginForm(request.POST)
        if login_form.is_valid():
            cd = login_form.cleaned_data
            user = authenticate(request, username=cd['username'], password=cd['password'])
            if user is not None:
                login(request, login)
                messages.success(request, 'Logged in successfully!', 'success')
                return redirect('home-page')
            else:
                messages.error(request, 'Username or password is wrong!', 'danger')
    else:
        login_form = forms.UserLoginForm()
    return render(request, 'login.html', {'login_form': login_form})
i have no idea why 'user' variable equals None!
'cd' works right (i have tested it)
help me!