[英]How to use UserCreationForm in Django?
I have followed this tutorial to test out the User authentication and Signals in Django.我按照本教程测试了 Django 中的用户身份验证和信号。 I don't know what I should do with this part (found from the first post of this tutorial):
我不知道我应该如何处理这部分(从本教程的第一篇文章中找到):
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class RegisterForm(UserCreationForm):
birthdate = forms.DateField()
discord_id = forms.CharField(max_length=100, help_text='Discord ID')
zoom_id = forms.CharField(max_length=100, help_text='Zoom ID')
text = forms.TextField(null=True, blank=True)
class Meta:
model = User
fields = ["username", "password1", "password2", "birthdate", "email", "discord_id", "zoom_id"]
With those imports I get an error NameError: name 'forms' is not defined
and if I add an import from django import forms
I get errors like AttributeError: module 'django.forms' has no attribute 'TextField'
.通过这些导入,我得到一个错误
NameError: name 'forms' is not defined
,如果我from django import forms
添加导入,我会收到类似AttributeError: module 'django.forms' has no attribute 'TextField'
错误。
Sohuld I add all the fields from my Model into this RegisterForm
-class I want to include to the registration process?因此,我是否将 Model 中的所有字段添加到我想要包含在注册过程中的
RegisterForm
类中? What do I do to the fields that are textFields in my Model?我该如何处理我的 Model 中的 textFields 字段?
I think you are trying to use CharField
instead of TextField
.我认为您正在尝试使用
CharField
而不是TextField
。 Django uses CharField
in its forms with default widget as TextArea
. Django 在其 forms 中使用
CharField
,默认小部件为TextArea
。
You can find more details here您可以在此处找到更多详细信息
https://docs.djangoproject.com/en/3.2/ref/forms/fields/#charfield https://docs.djangoproject.com/en/3.2/ref/forms/fields/#charfield
Instead of using forms.TextField
which does not exist in Django, you need to use forms.CharField(widget=forms.Textarea)
.而不是使用
forms.TextField
中不存在的 forms.TextField ,您需要使用forms.CharField(widget=forms.Textarea)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.