Python 3.x 电子邮件字段未保存在管理站点中

Python 3.x 电子邮件字段未保存在管理站点中,python-3.x,django-2.1,Python 3.x,Django 2.1,当我在django 2.1中尝试以下代码时,电子邮件字段没有保存在管理站点中。只有用户名在保存。我可以在自定义表单中创建电子邮件字段。因此,任何人都可以帮助我解决此问题。 提前谢谢 views.py ''' ''' forms.py ''' “”“我找到了答案。在forms.py中,我们想用大写字母m来表示元类,而不是用小写字母m。这是错误。在forms.py中,我们想用大写字母m来表示元类,而不是用小写字母m。这是错误 from django.contrib.auth.forms import

当我在django 2.1中尝试以下代码时,电子邮件字段没有保存在管理站点中。只有用户名在保存。我可以在自定义表单中创建电子邮件字段。因此,任何人都可以帮助我解决此问题。 提前谢谢

views.py '''

''' forms.py '''


“”“

我找到了答案。在forms.py中,我们想用大写字母m来表示元类,而不是用小写字母m。这是错误。在forms.py中,我们想用大写字母m来表示元类,而不是用小写字母m。这是错误

from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django import forms
from django.core.exceptions import ValidationError


class CustomUserCreationForm(forms.Form):
    username = forms.CharField(label='Enter Username', min_length=4, max_length=150)
    email = forms.EmailField(label='Enter email')
    password1 = forms.CharField(label='Enter password', widget=forms.PasswordInput)
    password2 = forms.CharField(label='Confirm password', widget=forms.PasswordInput)

    def clean_username(self):
        username = self.cleaned_data['username'].lower()
        r = User.objects.filter(username=username)
        if r.count():
            raise  ValidationError("Username already exists")
        return username

    def clean_email(self):
        email = self.cleaned_data['email'].lower()
        r = User.objects.filter(email=email)
        if r.count():
            raise  ValidationError("Email already exists")
        return email

    def clean_password2(self):
        password1 = self.cleaned_data.get('password1')
        password2 = self.cleaned_data.get('password2')

        if password1 and password2 and password1 != password2:
            raise ValidationError("Password don't match")

        return password2

    def save(self, commit=True):
        user = User.objects.create_user(
            self.cleaned_data['username'],
            self.cleaned_data['email'],
            self.cleaned_data['password1']
        )
        return user
from django.forms import EmailField
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm

class UserRegisterForm(UserCreationForm):
    email = EmailField(required=True)


    class meta:
        model = User
        fields = ["username", "email", "password1", "password2"]