Error while doing POST 'UserCreationForm' object has no attribute 'is_vaild'

379 Views Asked by At

I'm a django learner and I was trying to create user registration form using the in-build UserCreationForm.

view.py

from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm

def register(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_vaild():
            username = form.cleaned_data['username']
            return redirect('blog-home')
    else:
        form = UserCreationForm()

    return render(request, 'users/register.html',{'form':form})

While trying to POST i'm receiving 'UserCreationForm' object has no attribute 'is_vaild'. If i understand correctly for all the django forms there will be a is_valid function to validate.

Please help me to find what am i missing here.

Let me know if you need any other file details.

I'm using Django 2.1,Python 3.6

2

There are 2 best solutions below

0
On

Please take in mind that you still need to send something in case the form is invalid !form.is_valid() added a few lines to your code, BTW your simply mispelled is_valid()

def register(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            return redirect('blog-home')
        else:
            # Show user form errors with {% form.errors %} on the template.
            return render(request, 'users/register.html', {'form': form})
    else:
        form = UserCreationForm()

    return render(request, 'users/register.html',{'form':form})
0
On

As Abdul Niyas said, change is_vaild to is_valid.