'RegisterForm' object has no attribute 'is_valid'

Viewed 521

This Is models.py

from django.db import models
        
class RegisterForm(models.Model):
    fname = models.CharField(max_length=100)
    lname = models.CharField(max_length=100)
    pno = models.CharField(max_length=100)
    email = models.EmailField(max_length=200)
    pass1 = models.CharField(max_length=100)
    pass2 = models.CharField(max_length=100)

This is my Views.py

from django.shortcuts import render
from .models import RegisterForm
# Create your views here.
def registerView(request):
    if request.method=='POST':
        fm = RegisterForm(request.POST)
        if fm.is_valid():
            fname = fm.cleaned_data['fname']
            lname = fm.cleaned_data['lanme']
            pno = fm.cleaned_data['pno']
            email = fm.cleaned_data['email']
            pass1 = fm.cleaned_data['pass1']
            pass2 = fm.cleaned_data['pass2']
            reg = RegisterForm.save(fname = fname,lname=lname,pno=pno,email=email,pass1=pass1,pass2=pass2)
            reg.save()
            fm = RegisterForm()
    else:
        fm = RegisterForm()
    return render(request, 'register.html', {})
        

Request Method: POST

Request URL: http://127.0.0.1:8000/

Django Version: 3.2

Exception Type: AttributeError

Exception Value: 'RegisterForm' object has no attribute 'is_valid'

Exception Location: H:\Django\authetication\users\views.py, line 7, in registerView

2 Answers

You created a model, not a form. Now you need to create forms.py in-app

from django import forms
from .models import Profile

class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = '__all__'    

You can use all for all fields in the model but you can put only chosen fields in the form via tuple ('field_name', 'field_name')

Your RegisterForm is a model, not a model form, hence RegisterForm(request.POST) makes no sense, and your RegisterForm has no .is_valid(…) method. A model deals with storing data in the database, whereas a form will receive, validate and clean data.

It looks like you want to define a profile, so you can implement a Profile model:

# app_name/models.py

from django.db import models
     
class Profile(models.Model):
    fname = models.CharField(max_length=100)
    lname = models.CharField(max_length=100)
    pno = models.CharField(max_length=100)
    email = models.EmailField(max_length=200)
    pass1 = models.CharField(max_length=100)
    pass2 = models.CharField(max_length=100)

and then define a ModelForm based on that model:

# app_name/forms.py

from django import forms

class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = '__all__'

Then in the view you can work with:

from django.shortcuts import render
from .forms import ProfileForm

def registerView(request):
    if request.method == 'POST':
        form = ProfileForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('name-of-some-view')
    else:
        form = ProfileForm()
    return render(request, 'register.html', {'form': form})

You however should probably make modifications to your model: only use one field where you store the password, and likely the password should be hashed to prevent a data leak in case data of your database got stolen. Django already has a user model, and thre documentation has a section named Customizing authentication in Django. That can help you define a model for a user, and register that user.


Note: In case of a successful POST request, you should make a redirect [Django-doc] to implement the Post/Redirect/Get pattern [wiki]. This avoids that you make the same POST request when the user refreshes the browser.

Related