Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在我的登记表中添加名字和姓氏_Python_Django - Fatal编程技术网

Python 如何在我的登记表中添加名字和姓氏

Python 如何在我的登记表中添加名字和姓氏,python,django,Python,Django,我正在与Django合作,我定制了身份验证系统,以注册电子邮件而不是用户名 我想在我的注册表格中添加名字和姓氏输入 这是我的密码 from django.db import models from django.contrib.auth.models import AbstractUser class CustomUser(AbstractUser): username = models.CharField(max_length=255, unique=False, blank=Tru

我正在与Django合作,我定制了身份验证系统,以注册电子邮件而不是用户名

我想在我的注册表格中添加名字和姓氏输入

这是我的密码

from django.db import models
from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser):
    username = models.CharField(max_length=255, unique=False, blank=True, null=True)
    email = models.EmailField('email address', unique=True)
    USERNAME_FIELD = 'email'   
    REQUIRED_FIELDS = ['username'] 
我已经做到了

<form method="post">
{% csrf_token %}
<input type="text" name="first_name">
<input type="text" name="last_name">
{{form.as_p}}
<input type="submit">
</form>

{%csrf_令牌%}
{{form.as_p}}

如您所见,我在表单中添加了两个html字段,但它不起作用,我的数据库表中的两列(first_name+last_name)仍然为空

AbstractUser
已经定义了first_name和last_name属性,因此重命名这些字段是多余的,除非添加其他参数

步骤1:创建扩展
UserCreationForm

从django.contrib.auth导入get\u user\u模型
从django.contrib.auth.forms导入UserCreationForm
类注册表单(UserCreationForm):
类元:
模型=获取用户模型()
字段=(“名”、“姓”,
“电子邮件”、“用户名”、“密码1”、“密码2”)
步骤2:创建使用自定义表单的视图

从django.views导入泛型
从.forms导入注册表单
类注册视图(generic.CreateView):
"""
允许用户创建新帐户
"""
form_class=注册表单
template\u name=“authentication\u APP/singup.html”
success\u url=reverse\u lazy('some:reversed\u url')
步骤3:创建用于注册用户的URL:

from.views导入注册视图
URL模式=[
路径(“signup/”,SignUpView.as_view(),name=“signup”),
]
现在,如果您尝试:


{%csrf_令牌%}
{{form.as_p}}
如果它不尝试写下每个字段,那么它应该可以工作

{{form.first{u name}
{{form.last_name}

我没有谈论添加额外字段@nonolondon链接您是否尝试将
'first\u name'
'last\u name'
添加到Python代码中的
必填字段列表中?是的,已经没有结果我认为您还需要添加
CustomUser
class 2行
first\u name=models.CharField()
last\u name=models.CharField()
尝试获取这两个值,但问题相同