I'm trying to set a sign up page for a project.
I created a custom user class adding few optional fields:
I'm using Django 1.5
and Python 2.7
class CustomUser(models.Model):
middleschool = 'MS'
highschool = 'HS'
university = 'U'
blank = '-'
male = 'M'
female = 'F'
school_choices = ((middleschool, 'Middle School'), (highschool, 'High school'), (university, 'University'), (blank, 'Not defined'),)
sex = ((male, 'Male'), (female, 'Female'), (blank, 'Not defined'),)
user = models.ForeignKey(User, unique=True)
school = models.CharField(max_length = 30, choices = school_choices, default = blank, blank=True, null=True)
birthdate = models.DateField(blank=True, null=True)
sex = models.CharField(max_length = 1, choices = sex, default = blank, blank=True, null=True)
city = models.CharField(max_length = 30, blank=True, null=True)
payment_info = models.BigIntegerField(max_length = 30, blank=True, null=True)
rating = models.DecimalField(max_digits=2, decimal_places=0, default = 0)
premiumstatus = models.BooleanField(default = False, blank=False, null=False)
and added a create_user
method as found here.
This is the model's method:
def create_user(sender, instance, created, **kwargs):
if created:
CustomUser.objects.create(user=instance)
post_save.connect(create_user, sender=User)
this is the relative view:
def registration(request):
if request.method == 'POST':
form = Registration(request.POST)
if form.is_valid():
cd =form.cleaned_data
fuser = cd['username']
fpassword = cd['password']
femail = cd['email']
fname = cd['name']
fsurname = cd['surname']
fschool = cd['school']
fbday = cd['birthdate']
fsex = cd['sex']
fcity = cd['city']
user = User.objects.create_user(fuser, fpassword, femail)
user.is_active = True
user.save()
return HttpResponseRedirect("/home/")
else:
form = Registration()
return render(request, "registration.html", {'form': form})
and the relative form:
class Registration(forms.Form):
middleschool = 'MS'
highschool = 'HS'
university = 'U'
blank = '-'
male = 'M'
female = 'F'
school_choices = ((middleschool, 'Middle School'), (highschool, 'High school'), (university, 'University'), (blank, 'Not defined'),)
sex = ((male, 'Male'), (female, 'Female'), (blank, 'Not defined'),)
username = forms.CharField(label = 'Username')
password = forms.CharField(label = 'Password', widget=forms.PasswordInput)
repassword = forms.CharField(label = ' Reinstert password', widget=forms.PasswordInput)
email = forms.EmailField()
name = forms.CharField(label = 'Name', required=False)
surname = forms.CharField(label = 'Surname', required=False)
school = forms.ChoiceField(choices = school_choices, required=False, label='What school are you enrolled?')
birthdate = forms.DateField(label = 'Birth date', required=False)
sex = forms.ChoiceField(choices = sex, required=False, label='Sex')
city = forms.CharField(label = 'City', required=False)
def clean_repassword(self):
repassword = self.cleaned_data['repassword']
password = self.cleaned_data['password']
if repassword != password:
raise forms.ValidationError("Verification password different from original password!")
widgets = {
'password': forms.PasswordInput(),
}
Now, the problem are basically two: I'm still a beginner so I dont' really understand how create_user
works, I tried to pass fname
and fsurname
as an argument of said method but it wont work, it tells me that only four arguments are accepted.
Second, if i try to add any optionl info with the command showed in the link:
user.get_profil().sex = fsex
it dosen't raise any error but doesn't work either.