首先上结论:
form适用于对单个表单的操作,并且需要对每个字段的验证规则自定义。
modelform:适用于对用户提交的单个表单操作,字段可以用model中的表的字段来作为验证规则,适用于快速的进行增加、修改。
formset:适用于对多个表单进行操作,字段需要也可以用model中的表的字段来作为验证规则。
modelfoemset:适用于对多个表单进行操作,字段需要也可以用model中的表的字段来作为验证规则,速度可能快一些(有待考证)
form用法
后端代码
from django.forms import widgetswid_01=widgets.TextInput(attrs={"class":"form-control"})wid_02=widgets.PasswordInput(attrs={"class":"form-control"})class UserForm(forms.Form): name=forms.CharField(max_length=32, widget=wid_01 ) pwd=forms.CharField(max_length=32,widget=wid_02) r_pwd=forms.CharField(max_length=32,widget=wid_02) email=forms.EmailField(widget=wid_01) tel=forms.CharField(max_length=32,widget=wid_01)def register(request): if request.method=="POST": form=UserForm(request.POST) if form.is_valid(): print(form.cleaned_data) # 所有干净的字段以及对应的值 else: print(form.cleaned_data) # print(form.errors) # ErrorDict : {"校验错误的字段":["错误信息",]} print(form.errors.get("name")) # ErrorList ["错误信息",] return HttpResponse("OK") form=UserForm() return render(request,"register.html",locals())
modelform用法
class MenuModelForm(forms.ModelForm): class Meta: model = models.Menu fields = '__all__' widgets = { 'title': forms.TextInput(attrs={'placeholder': '请输入角色名称', 'class': 'form-control'}), 'icon': forms.RadioSelect( choices=ICON_LIST ) } error_messages = { 'title': { 'required': '菜单名称不能为空', }, 'icon': { 'required': '请选择图标', } }def menu_add(request): """ 添加菜单 :param request: :return: """ if request.method == 'GET': form = MenuModelForm() else: form = MenuModelForm(request.POST) if form.is_valid(): print(form.data) form.save() return redirect(reverse('rbac:menu_list')) return render(request, 'rbac/menu_change.html', {'form': form})def menu_edit(request, pk): """ 编辑菜单 :param request: :return: """ obj = models.Menu.objects.filter(id=pk).first() if not obj: return HttpResponse('菜单不存在') if request.method == 'GET': form = MenuModelForm(instance=obj) return render(request, 'rbac/menu_change.html', {'form': form}) form = MenuModelForm(request.POST, instance=obj) if form.is_valid(): form.save() return redirect(reverse('rbac:menu_list'))
formset用法
class MultiPermissionForm(forms.Form): id = forms.IntegerField( widget=forms.HiddenInput(), required=False ) title = forms.CharField( widget=forms.TextInput(attrs={'class': "form-control"}) ) url = forms.CharField( widget=forms.TextInput(attrs={'class': "form-control"}) ) name = forms.CharField( widget=forms.TextInput(attrs={'class': "form-control"}) ) menu_id = forms.ChoiceField( choices=[(None, '-----')], widget=forms.Select(attrs={'class': "form-control"}), required=False, ) pid_id = forms.ChoiceField( choices=[(None, '-----')], widget=forms.Select(attrs={'class': "form-control"}), required=False, ) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['menu_id'].choices += models.Menu.objects.values_list('id', 'title') self.fields['pid_id'].choices += models.Permission.objects.filter(pid__isnull=True).exclude( menu__isnull=True).values_list('id', 'title') def clean_pid_id(self): menu = self.cleaned_data.get('menu_id') pid = self.cleaned_data.get('pid_id') if menu and pid: raise forms.ValidationError('菜单和根权限同时只能选择一个') return piddef add(request): """ 增加 :param request: :return: """ MultiPermissionFormSet = formset_factory(MultiPermissionForm, extra=0) if request.method == 'GET': form = MultiPermissionFormSet() return render(request,'list.html',{'form':form} form = MultiPermissionFormSet(request.post) if form.is_valid(): return redict('url') else: .........def edit(request): """ 修改 :param request: :return: """ MultiPermissionFormSet = formset_factory(MultiPermissionForm, extra=0) if request.method == 'GET': form = MultiPermissionFormSet('数据') # formset是支持批量修改的所以 这里传入的数据一定要是可迭代对象,并且里面的 数据类型是字典或者对象 return render(request,'list.html',{'form':form} form = MultiPermissionFormSet(request.post) if form.is_valid(): return redict('url') else: .........
modelformset用法
class StudyRecordModelForm(forms.ModelForm): class Meta: model = models.StudyRecord fields = ['student','record','score','homework_note'] def changelist_view(self,request): ccid = request.GET.get('ccid') model_formset_cls = modelformset_factory(models.StudyRecord,StudyRecordModelForm,extra=0) queryset = models.StudyRecord.objects.filter(course_record_id=ccid) if request.method == "GET": formset = model_formset_cls(queryset=queryset) # 这里UI定是个可迭代对象,因为modelformset是操作多表的,里面的数据类型可以为字典或者对象 return render(request,'study_record.html',{'formset':formset}) formset = model_formset_cls(data=request.POST) print(request.POST) if formset.is_valid(): formset.save() return redirect('/stark/crm/studyrecord/list/?ccid=%s' %ccid ) return render(request, 'study_record.html', {'formset': formset})
学习记录