Django authenticate problem


Joined
Oct 19, 2022
Messages
1
Reaction score
0
this is my form in form.py:
Python:
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:
Python:
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!
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top