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

查看:24
本文介绍了在 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

模型 - 这是我的用户配置文件,我想用它来补充用户.

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

推荐答案

制作两种模型表单,一种用于User模型,一种用于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']

给它们加上前缀并将它们一起显示在同一个 HTML 表单元素中.在视图中,在保存 User 和 UserProfile 之前检查两者是否有效.

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天全站免登陆