gpt4 book ai didi

python - 如何在 Django 中进行表单验证

转载 作者:行者123 更新时间:2023-11-28 18:21:42 25 4
gpt4 key购买 nike

我正在尝试进行表单验证,以便某些字段只接受某些类型的输入,例如姓名只接受字母,电话号码只接受 10 个数字。我尝试对名字进行验证,如下所示:

#forms.py

class StudentForm(forms.ModelForm):
# STEP 1 FORM
student_id = forms.CharField(max_length=128, label="Student ID")
first_name = forms.CharField(max_length=128, label="First Name", widget=forms.TextInput(attrs={'class': 'form-control'}))
last_name = forms.CharField(max_length=128, label="Last Name")
ssn = USSocialSecurityNumberField(widget=forms.TextInput(attrs={'class': 'form-control'}), label="SSN", help_text="Format: xxx-xx-xxxx")
gender = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control'}), label="Gender", choices=GENDER_CHOICES)
dob = forms.DateField(widget=forms.DateInput(attrs={'class': 'form-control'}), label="Date of birth", help_text="Format: yyyy-mm-dd")
contact_number = forms.CharField(max_length=128, label="Contact number")
address = forms.CharField(max_length=128, label="Address")
city = forms.CharField(max_length=128, label="City")
state = forms.ChoiceField(choices=STATE_CHOICES, initial="NJ", label="State")
zipcode = USZipCodeField(label="Zipcode")
country = forms.ChoiceField(choices=countries, label="Country", initial="US")
home_phone = forms.CharField(max_length=128, label="Home phone")
cell_phone = forms.CharField(max_length=128, label="Cell phone")
email = forms.EmailField(widget=forms.EmailInput(attrs={'class': 'form-control'}), max_length=254, validators=[validate_email], label="Email")
background = forms.ChoiceField(choices=BACKGROUND_CHOICES, label="Background")
location = forms.ChoiceField(choices=LOCATION_CHOICES, initial="south_plainfield", label="Location")
workforce = forms.ChoiceField(choices=WORKFORCE_CHOICES, initial="--", label="Workforce")
source = forms.ChoiceField(choices=SOURCE_CHOICES, initial="individual", label="Source")
refer_by = forms.ChoiceField(choices=REFER_BY_CHOICES, initial="no refer", label="Refer by")
last_status = forms.ChoiceField(choices=LAST_STATUS_CHOICES, initial="followup", label="Last status")
newsletter = forms.BooleanField(widget=forms.CheckboxInput(), label="Newsletter", required=False)
created_by = forms.CharField(max_length=128, label="Created by")
date = forms.DateField(widget=forms.DateInput(attrs={'class': 'form-control'}), label="Date", help_text="Format: yyyy-mm-dd")
notes = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control'}), required=False, label="notes", help_text="less than 1000 characters")

def clean(self):
cleaned_data = self.cleaned_data
first_name = cleaned_data.get('first_name')

if first_name.isalpha == False:
raise forms.ValidationError('Please enter a real name.')
elif first_name[0].isupper() == False or first_name[1:].isupper() == True:
raise forms.ValidationError('Please capitalize properly')
else:
cleaned_data['first_name'] = first_name

return cleaned_data

class Meta:
model = Student
fields = ('student_id', 'first_name', 'last_name', 'ssn', 'gender', 'dob', 'contact_number', 'address', 'city', 'state', 'zipcode', 'country', 'home_phone', 'cell_phone', 'email', 'background', 'location', 'workforce', 'source', 'refer_by', 'last_status', 'newsletter', 'created_by', 'date', 'notes')

但是,当提交表单时,它完全忽略了它,我的数据库浏览器显示可以接受一个带有数字的名称。是不是我哪里写错了,还是说除了forms.py,我还要改其他地方的代码?我觉得如果我能解决这个验证,那么其他验证应该更容易,但如果有人能告诉我确保表单中的电话号码只有 10 个数字的正确方法,那也很好。谢谢。

最佳答案

你不调用方法 isalpha:

if first_name.isalpha == False:

您创建了一个内置方法对象 isalpha,它永远不是 False。我认为这只是一个错字,这样写:

if first_name.isalpha():
... # your code

要验证电话号码,请使用方法 clean_phone:

def clean_phone(self):
phone = self.cleaned_data['phone']
if not phone.isdigit():
raise forms.ValidationError('Phone number can only contains digits')
elif len(phone) != 10:
raise forms.ValidationError('Length of phone number must be 10 digits')
return phone

编辑

完全正确,您不必在 forms.py 文件中描述字段,您应该在 models.py 中进行描述,但是一些特殊的,例如 first_name 必须只包含您可以在 forms.py 中描述的字母。

首先,在 models.py 中创建一个具有所有数据库约束(字段类型、最大长度、选择、唯一值等)的 Student 模型。考虑到 django 会自动创建 id 字段。

# models.py

class Student(models.Model):
first_name = models.CharField(max_length=128)
last_name = models.CharField(max_length=128)
# Type of gender field depends on type of keys you used in GENDER_CHOICES
# CharField for chars, IntegerField for integers
gender = models.CharField(choices = GENDER_CHOICES, max_length=5)
...
# Here you could validate your phone number length
# But you still have to check it for valide symbols (in forms.py)
phone = models.CharField(max_length = 10)
# and so on for all your fields

之后,在 forms.py 中定义您的 StudetForm。您不必用约束来描述所有学生字段。

# forms.py

from models import Student

class StudentForm(forms.ModelForm):
class Meta:
model = Student
fields = '__all__' # Django adds all fields you've described in Student
# Then you could rename you fields labels, otherwise django will use names of model fields
labels = {
'first_name': 'First Name',
'last_name': 'Last Name',
...
}
# And you also can override widgets for rendering.
# This feature could be useful for dates, because django default widget for DateField is TextInput.
widgets = {
'date': forms.SelectDateWidget,
}

# Now you could describe all validation methods
def clean_first_name(self):
first_name = self.cleaned_data['first_name']
if not first_name.isalpha():
return ValidationError('First name must contain only letters')
return first_name

关于python - 如何在 Django 中进行表单验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44749156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com