在Django中注册用户的最佳方式 [英] Best way to do register a user in Django

查看:117
本文介绍了在Django中注册用户的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Django应用程序实现用户注册。我读过的书提到了UserCreationForm,但是我不仅需要名字,密码和电子邮件地址。我可以实现我自己的用户对象,并使用我认为的ModelForm,但是后来我输掉了一些django方便的身份验证。最后,我读了一些关于UserProfiles的补充用户我猜,所以我想我需要所有这些东西的组合。这是我到目前为止。

I'm trying to implement User Registration for my Django app. The book I read mentions UserCreationForm but I need more than just name, password, and email address. I could implement my own user object and use ModelForm I think, but then I lose out on some of the django conveniences for authentication. Finally, I read something about UserProfiles which supplement Users I guess, so I think I need some combination of all of these things. Here's what I have so far.

查看 - 这很简单。所有我想在这里做的是创建我的表单并保存用户

View - this is simple enough. All I want to do here is create my form and save the user

def main(request):
    rform1 = forms.RegisterForm1()
    rform2 = forms.RegisterForm2()
    return render_to_response("authentication/index.html", {'form1': rform1, 'form2':rform2})

def register(request):
    if request.method == 'POST':
        rform1 = forms.RegisterForm1(request.POST)
        rform2 = forms.RegisterForm2(request.POST)
        if rform1.is_valid() and rform2.is_valid():
            new_user = rform1.save()
            return HttpResponseRedirect("/register-success/")
    return render_to_response("authentication/index.html", {'form1': rform1,'form2':rform2})

表单 - 这是我的创建用户的表单。或者开始至少

Form - this is my form for creating a user. Or the start of it at least

class RegisterForm1(forms.ModelForm):
    class Meta:
        model = User

class RegisterForm2(forms.ModelForm):
    class Meta:
        model = UserProfile

模型 - 这是我要补充用户的UserProfile。

Model - this is my UserProfile that I want to supplement User with.

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    phonenumber = PhoneNumberField()

是否清楚我想要做什么?我在正确的轨道上

Is it clear what I'm trying to do? Am I on the right track

推荐答案

使两个模型表单,一个用于用户模型,一个用于 UserProfile 模型。

Make two model forms, one for the User model and one for the UserProfile model.

class UserForm(django.forms.ModelForm):
    class Meta:
        model = User  

class UserProfileForm(django.forms.ModelForm):
    class Meta:
        model = UserProfile
        exclude = ['user']



Give them prefixes and display them together in the same HTML form element. In the view, check that both are valid before you save the User and then the UserProfile.

def register(request):
    if request.method == 'POST':
        uf = UserForm(request.POST, prefix='user')
        upf = UserProfileForm(request.POST, prefix='userprofile')
        if uf.is_valid() * upf.is_valid():
            user = uf.save()
            userprofile = upf.save(commit=False)
            userprofile.user = user
            userprofile.save()
            return django.http.HttpResponseRedirect(…something…)
    else:
        uf = UserForm(prefix='user')
        upf = UserProfileForm(prefix='userprofile')
    return django.shortcuts.render_to_response('register.html', 
                                               dict(userform=uf,
                                                    userprofileform=upf),
                                               context_instance=django.template.RequestContext(request))

register.html

<form method="POST" action="">
    {% csrf_token %}
    {{userform}}
    {{userprofileform}}
    <input type="submit">
</form>

这篇关于在Django中注册用户的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆