在django中添加自定义用户注册字段

时间:2016-09-21 12:51:31

标签: python django forms registration

我找不到太多信息/在向django create_user函数添加自定义用户字段时遇到问题。我有很多字段,我不知道如何将它们放入数据库,因为目前这个函数只允许用户名,密码,名字和姓氏。我的观点/表格/模型是:

的观点:

def create_account(request):
    form = CreateAccountForm(request.POST)
    if form.is_valid():
        username = form.cleaned_data['username']
        password = form.cleaned_data['password']
        password2 = form.cleaned_data['password2']
        first_name = form.cleaned_data['first_name']
        last_name = form.cleaned_data['last_name']
        gender = form.cleaned_data['gender']
        medication = form.cleaned_data['medication']
        medical_history = form.cleaned_data['medical_history']
        DOB = form.cleaned_data['DOB']
        email = form.cleaned_data['email']
        telephone = form.cleaned_data['telephone']
        address = form.cleaned_data['address']
        city = form.cleaned_data['city']
        state = form.cleaned_data['state']
        postcode = form.cleaned_data['postcode']

        if password == password2:
            if (password_verification(password)) == 3:
                if (username_verification(username)) == False:
                    user = User.objects.create_user(username, email, password)
                    user.last_name = last_name
                    user.first_name = first_name
                    user.save()
                    return HttpResponseRedirect('/login')
                else:
                    return HttpResponseRedirect('/create_account')
            else:
                return HttpResponseRedirect('/create_account')
        else:
            return HttpResponseRedirect('/create_account')
    return render(request, "create_account.html", {'form': form})

型号:

class user(models.Model):
    username = models.CharField(max_length=20)
    password = models.CharField(max_length=15)
    password2 = models.CharField(max_length=20)
    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=20)
    gender = models.CharField(max_length=20)
    medication = models.CharField(max_length=50, blank=True)
    medical_history = models.CharField(max_length=50,blank=True)
    DOB = models.CharField(max_length=20)
    email = models.EmailField(max_length=30)
    telephone = models.CharField(max_length=20)
    address = models.CharField(max_length=30)
    city = models.CharField(max_length=20)
    state = models.CharField(max_length=20)
    postcode = models.CharField(max_length=30)

形式:

class CreateAccountForm(forms.Form):
    username = forms.CharField()
    password = forms.CharField(widget=forms.PasswordInput)
    password2 = forms.CharField(widget=forms.PasswordInput)
    first_name = forms.CharField()
    last_name = forms.CharField()
    gender = forms.CharField()
    medication = forms.CharField()
    medical_history = forms.CharField()
    DOB = forms.CharField()
    email = forms.CharField()
    telephone = forms.CharField()
    address = forms.CharField()
    city = forms.CharField()
    state = forms.CharField()
    postcode = forms.CharField()

如果有人知道如何将额外的字段添加到数据库中,我将非常感激!

1 个答案:

答案 0 :(得分:1)

如果要扩展用户,则无法使用username作为char字段创建模型。只需按照Django Docs进行操作即可。