Django创建自定义UserCreationForm [英] Django create custom UserCreationForm

查看:255
本文介绍了Django创建自定义UserCreationForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django中启用了用户认证模块,但是当我使用 UserCreationForm 时,他只会询问用户名和两个密码/密码确认字段。我还要电子邮件和全名字段,并设置为必填字段。



我已经这样做了:



<$ p来自django.contrib.auth.forms导入的$ p> 导入来自django的UserCreationForm
import form
from django.contrib.auth.models import User

class RegisterForm(UserCreationForm):
email = forms.EmailField(label =Email)
fullname = forms.CharField(label =Full name)

class Meta:
model = User
fields =(username,fullname,email)

现在表单显示新的字段,但不保存在数据库中。



如何修复?

解决方案

用户模型中没有这样的字段称为 fullname



如果您希望使用原始模型存储名称,那么您必须将其单独存储为名字和姓氏。



编辑:如果你只想要一个fi eld的形式,仍然使用原始的用户模型使用以下:



你可以这样做:

 从django.contrib.auth.forms导入UserCreationForm 
from django import forms
from django.contrib.auth.models import User

class RegisterForm(UserCreationForm):
email = forms.EmailField(label =Email)
fullname = forms.CharField(label =First name)

class Meta :
model =用户
fields =(username,fullname,email)

现在你必须做manji所说的并覆盖save方法,但是由于User模型没有一个全名字段,它应该是这样的:


$ b $
user = super(RegisterForm,self).save(commit = False)
first_name,last_name = self.cleaned_data [fullname]。split()
user.first_name = first_name
user.la st_name = last_name
user.email = self.cleaned_data [email]
如果提交:
user.save()
返回用户

注意:您应该为全名字段添加一个干净的方法,以确保输入的全名仅包含两个部分,名字和姓氏,以及否则有效的字符。



用户模型的参考源代码:

  http:/ /code.djangoproject.com/browser/django/trunk/django/contrib/auth/models.py#L201 


I enabled the user auth module in Django, but when I use UserCreationForm he ask me only username and the two password/password confirmation fields. I want also email and fullname fields, and set to required fields.

I've done this:

from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.contrib.auth.models import User

class RegisterForm(UserCreationForm):
    email = forms.EmailField(label = "Email")
    fullname = forms.CharField(label = "Full name")

    class Meta:
        model = User
        fields = ("username", "fullname", "email", )

Now the form show the new fields but it doesn't save them in database.

How can I fix?

解决方案

There is no such field called fullname in the User model.

If you wish to store the name using the original model then you have to store it separately as a first name and last name.

Edit: If you want just one field in the form and still use the original User model use the following:

You can do something like this:

from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.contrib.auth.models import User

class RegisterForm(UserCreationForm):
    email = forms.EmailField(label = "Email")
    fullname = forms.CharField(label = "First name")

    class Meta:
        model = User
        fields = ("username", "fullname", "email", )

Now you have to do what manji has said and override the save method, however since the User model does not have a fullname field it should look like this:

def save(self, commit=True):
        user = super(RegisterForm, self).save(commit=False)
        first_name, last_name = self.cleaned_data["fullname"].split()
        user.first_name = first_name
        user.last_name = last_name
        user.email = self.cleaned_data["email"]
        if commit:
            user.save()
        return user

Note: You should add a clean method for the fullname field that will ensure that the fullname entered contains only two parts, the first name and last name, and that it has otherwise valid characters.

Reference Source Code for the User Model:

http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/models.py#L201

这篇关于Django创建自定义UserCreationForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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