Django创建一个包含额外字段的寄存器页面

时间:2017-12-06 11:03:49

标签: django django-models django-forms

我正在尝试创建一个位于www.mysite.com/register的注册页面,其中包含“用户名,电子邮件,密码”,通常和其他(性别,日期,工作位置)。首先,我尝试使用此代码,但我失败了。如何创建使用相同用户的Profile和RegisterPage?

在我的accounts / forms.py

class RegisterForm(forms.ModelForm):
username = forms.CharField(max_length=100, label='Kullanıcı Adı')
 password1 = forms.CharField(max_length=100, label='Parola', widget=forms.PasswordInput)
 password2 = forms.CharField(max_length=100, label='Parola Doğrulama', widget=forms.PasswordInput)
 GENDER_CHOICES = (
    ('M', 'Male'),
    ('F', 'Female'),
)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
class Meta:         model = User
       fields = [
        'username',
        'password1',
        'password2',
        'gender',

        ]
我的帐户/ views.py

中的

def register_view(request):
    form = RegisterForm(request.POST or None)
    if form.is_valid():
        user = form.save(commit=False)
        password = form.cleaned_data.get('password1')
        user.set_password(password)
        # user.is_staff = user.is_superuser = True
        user.save()
        new_user = authenticate(username=user.username, password=password)
        login(request, new_user)
        return redirect('home')

    return render(request, "accounts/form.html", {"form": form, 'title': 'Üye Ol'})

0 个答案:

没有答案