简体   繁体   中英

How to use UserCreationForm in Django?

I have followed this tutorial to test out the User authentication and Signals in 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' .

Sohuld I add all the fields from my Model into this RegisterForm -class I want to include to the registration process? What do I do to the fields that are textFields in my Model?

I think you are trying to use CharField instead of TextField . Django uses CharField in its forms with default widget as TextArea .

You can find more details here

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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM