이 오류가 있습니다-> raise TypeError ( " 'class Meta'got invalid attribute (s) : % s"% ','. join (meta_attrs)) TypeError : 'class Meta'got invalid attribute (s) : models
사람들이 등록 할 수있는 페이지를 만들고 싶지만이 오류가 있습니다. 어떻게 고쳐?
나는 구글을 검색했지만 이유를 찾을 수 없었다. 뭐가 문제 야?
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})
여기에서를 정의하고 Model
있지만 ModelForm
. 참고 그 Meta
의 ModelForm
사용 model
,하지 :model
s
from django import forms
from django.contrib.auth.models import User
class SignUp(forms.ModelForm):
class Meta:
model = User
fields = '__all__'
User
객체 를 생성하는 경우 데이터베이스에 해시 된 비밀번호를 UserCreationForm
저장하므로 에서 상속하는 것이 좋습니다 .
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class SignUp(UserCreationForm):
class Meta:
model = User
fields = '__all__'
generic을 사용 ModelForm
하면 원시 암호를 양식에 저장 한 다음 인증이 실패합니다 (해시 된 암호를 확인하기 때문에).
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다