我只是在写一个表格。但是我想在hostname
. 然后验证它if type = A
是一个域名,否则type = PTR
验证它是一个IP addres
s。这个逻辑会在表单或视图中完成吗?
RECORD_CHOICES = (
('A','A'),
('Cname','CNAME'),
('PTR', 'PTR'),
)
class CacheCheck(forms.Form):
type = forms.TypedChoiceField(choices=formfields.TYPE_CHOICES, initial='FIXED')
record = forms.TypedChoiceField(choices=formfields.RECORD_CHOICES, initial='FIXED')
hostname = forms.CharField(max_length=100)
def clean(self):
cleaned_data = super(CacheCheck, self).clean()
record = cleaned_data.get("record")
if record == "PTR":
hostname = forms.GenericIPAddressField(label=("ip address"))
else record == "A":
hostname = forms.RegexField(label=("hostname"), max_length=31, regex=r'[a-zA-Z0-9-_]*\.[a-zA-Z]{2,6}'
传递给 CacheCheck 类的 forms.Form 是混合形式还是子类化形式?