Note that there are some explanatory texts on larger screens.

plurals
  1. PORegistration of a custom user in Django
    text
    copied!<p>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 <code>Django 1.5</code> and <code>Python 2.7</code></p> <pre><code>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) </code></pre> <p>and added a <code>create_user</code> method as found <a href="https://stackoverflow.com/questions/14857719/django-user-creation-with-custom-user-model-results-in-internal-error" title="here">here</a>. This is the model's method:</p> <pre><code>def create_user(sender, instance, created, **kwargs): if created: CustomUser.objects.create(user=instance) post_save.connect(create_user, sender=User) </code></pre> <p>this is the relative view:</p> <pre><code>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}) </code></pre> <p>and the relative form:</p> <pre><code>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(), } </code></pre> <p>Now, the problem are basically two: I'm still a beginner so I dont' really understand how <code>create_user</code> works, I tried to pass <code>fname</code> and <code>fsurname</code> 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:</p> <pre><code>user.get_profil().sex = fsex </code></pre> <p>it dosen't raise any error but doesn't work either.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload