0
フォームの検証に問題があります。電子メールが存在するかどうかを確認するメソッドを作成しました。存在する場合はデータをデータベースに保存できませんが、個人的なケースDjango - forms.pyの有効化の問題
models.py:
from django.db import models
from promSpace.models import Space
class StudentRegistration(models.Model):
email = models.EmailField(max_length=50)
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
password = models.CharField(max_length=20, default="", null=False)
prom_code = models.CharField(max_length=8, default="", null=False)
gender = (
("M","Male"),
("F","Female"),
)
gender = models.CharField(max_length=1, choices=gender, default="M", null=False)
prom_name = models.CharField(max_length=20, default="N/A")
forms.py:
それはここで は私のコードでとにかくそれを保存しますfrom django import forms
from django.contrib.auth.models import User
#from models import StudentRegistration
from django.forms import ModelForm
from promSpace.models import Space
from StudentUsers.models import StudentRegistration
from django.contrib.auth.forms import UserCreationForm
class MyRegistrationForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
password2 = forms.CharField(widget=forms.PasswordInput)
prom_code = forms.CharField(max_length = 8)
email = forms.EmailField(max_length=50)
def email_al_exist(self):
email = self.cleaned_data['email']
if StudentRegistration.objects.filter(email = email).exists():
raise ValidationError("Email already exists")
return email
def clean_password2(self):
password = self.cleaned_data.get('password')
password2 = self.cleaned_data.get('password2')
if password and password2:
if password != password2:
raise forms.ValidationError(("The two password fields didn't match."))
return password2
def prom_code_exist(self):
prom_code_value = self.cleaned_data['prom_code']
prom_code_ver = Space.objects.get(prom_code = prom_code_value)
if Space.objects.filter(prom_code = prom_code_value).exists():
return prom_code_value
else:
raise forms.ValidationError(("Error, Code doesn't exist."))
class Meta:
model = StudentRegistration
fields = ('email', 'first_name', 'last_name','gender','prom_code','password')
観察:ユーザーがここに作成イム自分のテーブル(StudentRegistration)に保存されている、彼らはあなたのemail_al_exist
方法を得るのが呼ばれることはありません