djangoのサインアップ(ユーザー登録)画面を独自に定義し、サインアップの際にemailアドレスやアイコン画像が必要なように改良しました。
その後自分で適当なユーザー名と画像とパスワード、存在するメールアドレス(gmail)を入力してユーザー登録しようとしたところ、emailアドレスの欄に「この項目は必須です」と出てしまいます。
![]
なぜ入力しているのに必須ですと出てフォームが送信できないのかわからず、手詰まりになってしまいました。
<追記>
models.py
python
1from django.db import models 2from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, UserManager 3from django.contrib.auth.validators import UnicodeUsernameValidator 4from django.utils import timezone 5from django.utils.translation import gettext_lazy as _ 6from django.core.mail import send_mail 7import uuid as uuid_lib 8 9# Create your models here. 10 11 12class User(AbstractBaseUser, PermissionsMixin): 13 """ユーザー AbstractUserをコピペし編集""" 14 15 uuid = models.UUIDField(default=uuid_lib.uuid4, 16 primary_key=True, editable=False) 17 username_validator = UnicodeUsernameValidator() 18 19 username = models.CharField( 20 _('username'), 21 max_length=150, 22 unique=True, 23 help_text=_( 24 'Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.'), 25 validators=[username_validator], 26 error_messages={ 27 'unique': _("A user with that username already exists."), 28 }, 29 ) 30 image = models.ImageField() 31 email = models.EmailField(_('email address'), unique=True, help_text='正しいemailアドレスを入力してください') 32 33 is_staff = models.BooleanField( 34 _('staff status'), 35 default=False, 36 help_text=_( 37 'Designates whether the user can log into this admin site.'), 38 ) 39 is_active = models.BooleanField( 40 _('active'), 41 default=True, 42 help_text=_( 43 'Designates whether this user should be treated as active. ' 44 'Unselect this instead of deleting accounts.' 45 ), 46 ) 47 date_joined = models.DateTimeField(_('date joined'), default=timezone.now) 48 49 objects = UserManager() 50 51 EMAIL_FIELD = 'email' 52 USERNAME_FIELD = 'username' 53 REQUIRED_FIELDS = ['email', 'image'] 54 55 class Meta: 56 verbose_name = _('user') 57 verbose_name_plural = _('users') 58 59 def clean(self): 60 super().clean() 61 self.email = self.__class__.objects.normalize_email(self.email) 62 63 def email_user(self, subject, message, from_email=None, **kwargs): 64 """Send an email to this user.""" 65 send_mail(subject, message, from_email, [self.email], **kwargs)
forms.py
python
1from django import forms 2from django.contrib.auth.forms import UserCreationForm 3# from django.contrib.auth.models import User 4from accounts.models import User 5 6class SignUpForm(UserCreationForm): 7 8 class Meta: 9 model = User 10 fields = ('username', 'email', 'image', 'password1', 'password2')
あなたの回答
tips
プレビュー