Home > Net >  model form has no attribute 'cleaned_data'
model form has no attribute 'cleaned_data'

Time:07-11

I want to sign in to be able to use the site. However, I'm having a problem: 'LoginForm' object has no attribute 'cleaned_data'. Please tell me how can I solve it. I apologize in advance for my English

My forms.py

class LoginForm(forms.Form):
    user_name = forms.CharField(max_length=20, widget=TextInput(attrs={'type':'text','class': 'form-control','placeholder': 'Input username'}))
    passWord = forms.CharField(max_length=25, widget=TextInput(attrs={'type':'password','class': 'form-control','placeholder': 'Input password'}))
    class Meta:
        fields = ['user_name', 'passWord']

My views.py

def login_view(request):
    template_name = 'main/login.html'
    action_detail = ''
    if request.method == "POST":
        form = LoginForm(request.POST)
        if form.is_valid:
            username = form.cleaned_data.get('user_name')
            password = form.cleaned_data.get('passWord')
            user = authenticate(request, username=username, password=password)
            if user is not None:
                login(request, user)
                return redirect('/')
            else:
                action_detail = 'invalid username or password'
    else:
        form = LoginForm()
    context={
            'title': 'Login',
            'form': form,
            'action_detail': action_detail,
        }
    return render(request, template_name, context)

CodePudding user response:

is_valid is a function. https://docs.djangoproject.com/en/4.0/ref/forms/api/#django.forms.Form.is_valid

You should call it.

if form.is_valid():
  • Related