ModelForm和ModelFormSet的用法
ModelForm的用法:
1 class GoodsModelForm(forms.ModelForm): 2 """ 3 ModelForm 4 """ 5 class Meta: 6 model=models.Goods 7 fields="__all__" 8 # 错误消息 9 error_messages={ 10 'name':{'required':'不能为空'} 11 } 12 # 插件 13 widgets={ 14 'source':forms.RadioSelect() 15 } 16 17 # 定义class属性 18 def __init__(self,*args,**kwargs): 19 super().__init__(*args,**kwargs) 20 for name,filed in self.fields.items(): 21 if name=='source': 22 # 根据打印数据额类型找到源码去掉-------------- 23 from django.forms.models import ModelChoiceField 24 filed.empty_label=None 25 else: 26 filed.widget.attrs['class']='form-control
ModelFormSet的用法:
1 def mult_add(request): 2 """ 3 批量添加 4 :param request: 5 :return: 6 """ 7 from django.forms.models import modelformset_factory 8 GoodsModelFormSet=modelformset_factory(model=models.Goods,form=GoodsModelForm,extra=3) 9 if request.method=='GET': 10 form_set=GoodsModelFormSet() 11 return render(request, 'mult_add.html', {'form_set': form_set}) 12 form_set=GoodsModelFormSet(data=request.POST) 13 if form_set.is_valid(): 14 form_set.save() 15 return redirect('/index/') 16 return render(request,'mult_add.html')
添加后端代码:
1 def add(request): 2 """ 3 添加 4 :param request: 5 :return: 6 """ 7 if request.method=="GET": 8 form=GoodsModelForm() 9 return render(request,'add.html',{'form':form}) 10 form=GoodsModelForm(data=request.POST) 11 print(form) 12 if form.is_valid(): 13 form.save() 14 return redirect('/index/') 15 return render(request, 'add.html', {'form': form})
添加前端代码:
1 <link rel="stylesheet" href="https://cdn.bootcss.com/twitter-bootstrap/4.1.3/css/bootstrap.css"> 2 <div class="container"> 3 <h1>批量增加商品</h1> 4 <form method="post"> 5 {% csrf_token %} 6 {{ form_set.management_form }} 7 <table class="table table-bordered table-hover"> 8 <thead> 9 <tr> 10 <th>商品名称</th> 11 <th>价格</th> 12 <th>来源</th> 13 <th>分类</th> 14 <th>库房</th> 15 </tr> 16 </thead> 17 <tbody> 18 {% for form in form_set %} 19 <tr> 20 {% for field in form %} 21 <td>{{ field}} {{ field.errors.0}}</td> 22 {% endfor %} 23 </tr> 24 {% endfor %} 25 </tbody> 26 </table> 27 <input type="submit" value="提交"> 28 </form> 29 </div>
修改后端代码:
1 def update(request,nid): 2 """ 3 修改 4 :param request: 5 :param nid: 6 :return: 7 """ 8 obj=models.Goods.objects.filter(id=nid).first() 9 if request.method=="GET": 10 form=GoodsModelForm(instance=obj) 11 return render(request,'update.html',{'form':form}) 12 form=GoodsModelForm(data=request.POST,instance=obj) 13 if form.is_valid(): 14 form.save() 15 return redirect('/index/') 16 return render(request, 'update.html', {'form': form})
修改前端代码:
1 <div class="container"> 2 <h1>添加商品</h1> 3 <form class="form-horizontal" method="post" novalidate> 4 {% csrf_token %} 5 {% for field in form %} 6 <div class="form-group"> 7 <label class="col-sm-2 control-label">{{ field.label }}</label> 8 <div class="col-sm-10"> 9 {{ field }} <span style="color: red;">{{ field.errors.0 }}</span> 10 </div> 11 </div> 12 {% endfor %} 13 14 <div class="form-group"> 15 <div class="col-sm-offset-2 col-sm-10"> 16 <input type="submit" class="btn btn-success" value="提 交"> 17 </div> 18 </div> 19 </form> 20 21 </div>