-
models.py
from django.db import models class Info(models.Model): name = models.CharField(max_length=64) sex = models.CharField(max_length=64) birthday = models.CharField(max_length=64) age=models.CharField(max_length=64) qualification=models.CharField(max_length=64) job=models.CharField(max_length=64) email=models.CharField(max_length=64,default='') class Hobby(models.Model): item=models.CharField(max_length=64)
-
form.py
from django import forms from app01 import models from django.core.exceptions import ValidationError class InfoForm(forms.Form): def validate_name(value): try: models.Info.objects.get(name=value) raise ValidationError('%s 的信息已经存在!'%value) except models.Info.DoesNotExist: pass sex_choice=((0,'男'), (1,'⼥'))#select的数据可以像这样写,也可以在另外⼀张表中动 态去拿 name = forms.CharField(validators=[validate_name],label='姓 名',error_messages={'required':'必填'}) age = forms.CharField(label='年龄',error_messages={'required':'必 填'}) # sex = forms.CharField(label='性别',error_messages= {'required':'必填',},) sex=forms.IntegerField(widget=forms.widgets.Select(choices=sex_choice , attrs={'class':'setform2'} )) birthday = forms.CharField(label='⽣⽇',error_messages= {'required':'必填'}) qualification = forms.CharField(label='学历',error_messages= {'required':'必填'},widget=forms.TextInput(attrs= {'class':'formset','placeholder':'本科' })) email=forms.EmailField(max_length=100,min_length=10) job = forms.CharField(label='⼯作',error_messages={'required':'必 填'}) def __init__(self,*args,**kwargs): super(Info_form,self).__init__(*args,**kwargs) self.fields['hobby']=forms.CharField(widget=forms.widgets.Select(choi ces=models.Hobby.objects.values_list('id','item')))
Django—Form表单综合应用(部分1)
最新推荐文章于 2023-07-31 19:27:52 发布