在Django中注册自定义用户

时间:2013-12-21 19:03:04

标签: python django

我正在尝试为项目设置注册页面。 我创建了一个自定义用户类,添加了几个可选字段 我正在使用Django 1.5Python 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)

并添加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是如何工作的,我试图传递fnamefsurname作为参数说方法,但它不会工作,它告诉我只接受四个参数。 其次,如果我尝试使用链接中显示的命令添加任何optionl信息:

user.get_profil().sex = fsex

它不会引发任何错误,但也不起作用。

1 个答案:

答案 0 :(得分:0)

  

user.get_profile()。sex = fsex

您必须将用户配置文件分配给变量才能对其进行修改:

profile = user.get_profile()
profile.sex = fsex
profile.save()

更新尝试在创建用户时明确指定字段键,如下所示:

user = User.objects.create_user(username=fuser, email=femail)
user.set_password(fpassword)
相关问题