2
votes

I tried adding another field (pin) in forms.py and models.py

The error i got was

"FieldError at /student/signup/ Cannot resolve keyword 'pin' into field. Choices are: date_joined, email, first_name, groups, id, is_active, is_staff, is_superuser, last_login, last_name, logentry, password, user_infos, user_permissions, username"

forms.py

 class RegisterForm(forms.Form):
    GRADE_CHOICES = ( 
                (9,'9'), (10,'10'), (11,'11'), (12,'12') , 
            )
    curr_year = date.today().year
    GRAD_YEAR_CHOICES = ( 
                (curr_year,curr_year), (curr_year+1,curr_year+1), (curr_year+2,curr_year+2), (curr_year+3,curr_year+3) , 
                 )
    first_name = forms.CharField(max_length = 25)
    last_name = forms.CharField( max_length = 25)
    emailid = forms.EmailField()
    passwd1 = forms.CharField(max_length=100,widget=forms.PasswordInput)
    passwd2 = forms.CharField(max_length=100,widget=forms.PasswordInput)
    gradyear = forms.ChoiceField( choices=GRAD_YEAR_CHOICES)
    grade = forms.ChoiceField( choices=GRADE_CHOICES)
    pin = forms.IntegerField()
    def clean(self):
        cleaned_data = super(RegisterForm, self).clean()
        print cleaned_data
        if cleaned_data['passwd1'] != cleaned_data['passwd2']:
            raise forms.ValidationError({'passwd1':['Password do not match']})

        if User.objects.filter(email=cleaned_data['emailid']).count():
            raise forms.ValidationError({'emailid':['Email already taken ']})
        if User.objects.filter(pin=cleaned_data['pin']).count():
            raise forms.ValidationError({'pin':['Pin already taken ']})

        return cleaned_data

views.py

 def signup(request):
        print "signup"
        if request.method == 'POST':
            print "post signup"
            form = RegisterForm(request.POST)
            try:
                if form.is_valid():
                    print form.cleaned_data
                    u = User.objects.create_user(form.cleaned_data['emailid'], form.cleaned_data['emailid'], form.cleaned_data['passwd1'] )
                    ui = UserInfo()
                    ui.user = u
                    ui.class_of = form.cleaned_data['gradyear']
                    ui.grade = form.cleaned_data['grade']
                    ui.balance = 0
                    print "Hi"
                    ui.pin = form.cleaned_data['pin']
                    print ui.pin
                    u.first_name = form.cleaned_data['first_name']
                    u.last_name = form.cleaned_data['last_name']
                    u.save()
                    ui.save()
                    user = authenticate(username=form.cleaned_data['emailid'], password=form.cleaned_data['passwd1'])
                    login(request,user)
                    print "after login in signup"
                    return redirect("/")


                else:
                    print "error"
                    print form.errors
            except:
                raise
                print "error here"
                print form.errors
                pass
                #return render(request, 'student/register.html', {'form': form})

        else:
            form = RegisterForm()

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

models.py:

 class UserInfo(models.Model):
    user = models.OneToOneField(User, related_name='user_infos')
    class_of = models.IntegerField()
    #username = user.username
    #fname = user.fname
    #lname = user.last_name
    #email = user.email
    #Staff = user.is_staff
    pub_date = models.DateTimeField( auto_now=True)
    grade = models.IntegerField()
    balance = models.DecimalField(max_digits=6, decimal_places=2)
    pin = models.IntegerField()
    #first_name = models.CharField(max_length = 25)

I don't think not be doing this right. Is there any way to add another column in a database another way?

1

1 Answers

0
votes

You have "pin" field in your UserInfo model, but in forms.py you are trying to filter User model:

if User.objects.filter(pin=cleaned_data['pin']).count():

User model does not have "pin" field, so you are getting that error message.

Also I suggest you to learn and start using ModelForms: https://docs.djangoproject.com/en/1.8/topics/forms/modelforms/