我正在嘗試爲項目設置註冊頁面。 我創建了一個自定義用戶類中添加一些可選字段: 我使用Django 1.5
和Python 2.7
在Django中註冊自定義用戶
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)
,並增加了create_user
方法發現here。 這是模型的方法:
def create_user(sender, instance, created, **kwargs):
if created:
CustomUser.objects.create(user=instance)
post_save.connect(create_user, sender=User)
這是相對的觀點:
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})
和相對形式:
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(),
}
現在,這個問題基本上有兩種:我仍然是初學者,所以我不'真正瞭解create_user
如何工作,我試圖通過fname
和fsurname
作爲所述方法的參數,但它會工作,它告訴我只有四個論點被接受。 其次,如果我嘗試添加與命令的任何optionl信息顯示,在鏈接:
user.get_profil().sex = fsex
忽略了最低提出任何錯誤,但也不管用。 。
其中您使用的是Django的版本? –
對不起,我會編輯。 – Higure
如果您使用的是Django1.5,爲什麼不創建自定義用戶?https://docs.djangoproject.com/en/dev/topics/auth/customizing/#a-full-example –