Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.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)如何修复此错误?_Python_Django_Django Models_Django Forms - Fatal编程技术网

类型错误:';类元';获取了无效的属性。python(django)如何修复此错误?

类型错误:';类元';获取了无效的属性。python(django)如何修复此错误?,python,django,django-models,django-forms,Python,Django,Django Models,Django Forms,我有这个错误--> raise TypeError(“'class Meta'具有无效属性):%s“%”,'.join(Meta_ATTR)) TypeError:“类元”具有无效属性:模型 我想创建一个人们可以注册的页面,但我有这个错误。如何修复? 我在谷歌上搜索,但找不到原因。有什么问题吗? forms.py from django import forms from django.contrib.auth.forms import UserCreationForm from django.

我有这个错误--> raise TypeError(“'class Meta'具有无效属性):%s“%”,'.join(Meta_ATTR)) TypeError:“类元”具有无效属性:模型

我想创建一个人们可以注册的页面,但我有这个错误。如何修复?

我在谷歌上搜索,但找不到原因。有什么问题吗?

forms.py

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.contrib.auth import password_validation
from django.db import models




class SignUp(models.Model):
    class Meta:
        models=User
models.py

from django.contrib.auth.models import AbstractUser, BaseUserManager
from django.db import models
from django.utils.translation import ugettext_lazy as _


class UserManager(BaseUserManager):
    """Define a model manager for User model with no username field."""

    use_in_migrations = True

    def _create_user(self, email, password, **extra_fields):
        """Create and save a User with the given email and password."""
        if not email:
            raise ValueError('The given email must be set')
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, email, password=None, **extra_fields):
        """Create and save a regular User with the given email and password."""
        extra_fields.setdefault('is_staff', False)
        extra_fields.setdefault('is_superuser', False)
        return self._create_user(email, password, **extra_fields)

    def create_superuser(self, email, password, **extra_fields):
        """Create and save a SuperUser with the given email and password."""
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError('Superuser must have is_staff=True.')
        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')

        return self._create_user(email, password, **extra_fields)


class User(AbstractUser):
    """User model."""

    username = None
    email = models.EmailField(_('email address'), unique=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = UserManager()
views.py

from . import forms
from django.shortcuts import render
from django.http import  HttpResponse
import datetime
from django.contrib.auth import authenticate


def regform(request):
    if request.method == 'POST':
        form = SignUp(request.POST)
        if form.is_valid():
            form.save()
            email = form.cleaned_data.get('email')
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(email=email, password=raw_password)
            login(request, user)
            return redirect('home')
    else:
        form = SignUp()
    return render(request, 'home/home.html', {'form': form})


您在这里定义的是
模型
,但您可能希望定义
模型表单
。请注意,
ModelForm
Meta
使用
model
,而不是
models


通过使用通用的
ModelForm
,它将在表单中存储原始密码,然后身份验证将失败(因为这将检查散列密码)。

您在这里定义的是
模型,但您可能希望定义
ModelForm
。请注意,
ModelForm
Meta
使用
model
,而不是
models


通过使用通用的
ModelForm
,它将在表单中存储原始密码,然后身份验证将失败(因为这将检查散列密码)。

它应该是ModelForm而不是models.Model>

from django.contrib.auth.models import User
from django.forms import ModelForm
class SignUp(ModelForm):
    class Meta:
        model=User
        fields = [add your fields here]

它应该是ModelForm而不是models.Model

from django.contrib.auth.models import User
from django.forms import ModelForm
class SignUp(ModelForm):
    class Meta:
        model=User
        fields = [add your fields here]

Model
在其
Meta
中没有
Model=
(也没有
models=
)。这应该是一个
ModelForm
@WillemVanOnsem类注册(models.ModelForm>):AttributeError:module'django.db.models'没有属性'ModelForm'。它是
forms.ModelForm
,而不是
models.ModelForm
Model
在其
中没有
Model=/code>。这应该是
ModelForm
@WillemVanOnsem类注册(models.ModelForm):AttributeError:module'django.db.models'没有属性'ModelForm',它是
forms.ModelForm
,而不是
models.ModelForm
。raise配置不正确(django.core.exceptions.impropertlyconfigured:禁止创建没有“fields”属性或“exclude”属性的ModelForm;表单注册需要更新。@kokiwebaa:当然,您需要指定要在
注册中使用哪些字段。我建议您阅读错误消息,而不是ust copying.form=SignUp()名称错误:未定义名称“SignUp”。@kokiwebaa:这是因为您忘记在
视图.py中导入此内容。raise配置不正确(django.core.exceptions.impropertlyconfigured:禁止创建没有“fields”属性或“exclude”属性的ModelForm;表单注册需要更新。@kokiwebaa:当然,您需要指定要在
注册中使用哪些字段。我建议您阅读错误消息,而不是ust copying.form=SignUp()名称错误:名称“SignUp”未定义。@kokiwebaa:那是因为您忘记在
视图.py中导入此项。form=SignUp()名称错误:名称“SignUp”未定义form=SignUp()名称错误:名称“SignUp”未定义
from django.contrib.auth.models import User
from django.forms import ModelForm
class SignUp(ModelForm):
    class Meta:
        model=User
        fields = [add your fields here]