Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.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 Django用户创建返回”;键';用户名';在'中找不到;CustomUserForm'&引用;对于自定义用户_Python_Django - Fatal编程技术网

Python Django用户创建返回”;键';用户名';在'中找不到;CustomUserForm'&引用;对于自定义用户

Python Django用户创建返回”;键';用户名';在'中找不到;CustomUserForm'&引用;对于自定义用户,python,django,Python,Django,我已经为django项目创建了一个自定义用户。当我尝试使用管理员创建新用户时,我无法访问该表单,并收到一个错误:在“CustomUserForm”中找不到键“username” 我的代码如下: models.py from django.core import validators from django.db import models from django.utils import timezone from django.utils.http import urlquote from d

我已经为django项目创建了一个自定义用户。当我尝试使用管理员创建新用户时,我无法访问该表单,并收到一个错误:
在“CustomUserForm”中找不到键“username”

我的代码如下:

models.py

from django.core import validators
from django.db import models
from django.utils import timezone
from django.utils.http import urlquote
from django.utils.translation import ugettext_lazy as _
from django.core.mail import send_mail
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin

from .managers import CustomUserManager

class CustomUser(AbstractBaseUser, PermissionsMixin):

    email = models.EmailField(_('email address'), max_length=254, unique=True)
    first_name = models.CharField(_('first name'), max_length=30, blank=True)
    last_name = models.CharField(_('last name'), max_length=30, blank=True)
    is_staff = models.BooleanField(_('staff status'), default=False,
        help_text=_('Designates whether the user can log into this admin '
                    'site.'))
    is_active = models.BooleanField(_('active'), default=True,
        help_text=_('Designates whether this user should be treated as '
                    'active. Unselect this instead of deleting accounts.'))
    date_joined = models.DateTimeField(_('date joined'), default=timezone.now)

    objects = CustomUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    class Meta:
        verbose_name = _('user')
        verbose_name_plural = _('users')

    def __unicode__(self):
        return self.email

    def get_absolute_url(self):
        return "/users/%s/" % urlquote(self.email)


    def get_full_name(self):
        """
        Returns the first_name and the last name
        with a space between them.
        """
        full_name = '%s %s' % (self.first_name, self.last_name)
        return full_name.strip()

    def get_short_name(self):
        "Returns a short name for the user."
        return self.first_name

    def email_user(self, subject, message, from_email=None):
        "Sends an email to the user."
        send_mail(subject, message, from_email, [self.email])
经理.py

from django.contrib.auth.models import BaseUserManager
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _

class CustomUserManager(BaseUserManager):

    def _create_user(self, email, password, is_staff, is_superuser, **extra_fields):

        now = timezone.now()

        if not email:
            raise ValueError(_(u'The given username must be set'))
        email = self.normalize_email(email)
        user = self.model(email=email,
                          is_staff=is_staff, is_active=True,
                          is_superuser=is_superuser, last_login=now,
                          date_joined=now, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, email, password=None, **extra_fields):
        return self._create_user(email, password, False, False,
                                 **extra_fields)

    def create_superuser(self, email, password, **extra_fields):
        return self._create_user(email, password, True, True,
                                 **extra_fields)
forms.py

from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.utils.translation import ugettext_lazy as _
from django import forms
from .models import CustomUser

class CustomUserCreationForm(UserCreationForm):
    """
    A form that creats a custom user with no privilages
    form a provided email and password.
    """

    def __init__(self, *args, **kargs):
        super(CustomUserCreationForm, self).__init__(*args, **kargs)
        del self.fields['username']

    class Meta:
        model = CustomUser
        fields = ('email',)

class CustomUserChangeForm(UserChangeForm):
    """
    A form for updating users. Includes all the fields on
    the user, but replaces the password field with admin's
    password hash display field.
    """

    def __init__(self, *args, **kargs):
        super(CustomUserChangeForm, self).__init__(*args, **kargs)
        del self.fields['username']

    class Meta:
        model = CustomUser
        fields = '__all__'
管理员

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.utils.translation import ugettext_lazy as _

from .models import CustomUser
from .forms import CustomUserChangeForm, CustomUserCreationForm

class CustomUserAdmin(UserAdmin):

    fieldsets = (
        (None, {'fields': ( 'email', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name')}),
        (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
                                       'groups', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )
    add_filedsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'password', 'password2')}
        ),
    )
    form = CustomUserChangeForm
    add_form = CustomUserCreationForm
    list_display = ('email', 'first_name', 'last_name', 'is_staff')
    search_fields = ('email', 'first_name', 'last_name')
    ordering = ('email',)

admin.site.register(CustomUser, CustomUserAdmin)
我在代码中找不到任何包含
CustomUserForm
的文件。我的设置是否有任何错误之处,或者我可以参考一些东西来处理这个问题

编辑: 这是完整的回溯


Django似乎正在这个实例CustomUserForm中寻找一个名为(custom_user_model_name)表单的表单。

问题是输入错误
add_filedsets
应该是
add_fieldsets

发布整个回溯消息,以便我们能够找到CustomUserForm所在的位置。我已将回溯作为编辑发布。如果CustomUserCreationForm中没有
del self.fields['username']
,则会加载一个表单,但它不是来自CustomUser模型。django的哪个版本?否,但该项目尚未完成,它似乎影响了我过去的一些项目,所以我会尽快让您知道我有一个解决方案。是的,这已解决错误是CustomUserAdmin add_filedsets to add_fieldsets中的一个输入错误