1
votes

this is my 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

                        ui.save()

and in my forms.py i have:

 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)

        def clean(self):
            cleaned_data = super(RegisterForm, self).clean()

            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 ']})

            return cleaned_data

why does everything print to the database except first_name and last_name??? (username, email, grade, gradyear, and password all save)

EDIT: This is my UserInfo

 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)
    #first_name = models.CharField(max_length = 25)
1
what does the UserInfo model look like?user1797792
just add User.objects.create_user(first_name=form.cleaned_data['first_name'], ...sobolevn
I want to add it to the database, not make it a part of of the login user @sobolevnAbhishek Patel

1 Answers

1
votes

In the code provided, you never save first_name and last_name for User or UserInfo.

In def signup(request):, right after this line:

u = User.objects.create_user(form.cleaned_data['emailid'], form.cleaned_data['emailid'], form.cleaned_data['passwd1'] )

Try including this:

u.first_name = form.cleaned_data['first_name']
u.last_name = form.cleaned_data['last_name']
u.save()