1.HTML
编辑和添加页面得内容相同,使用include将他们整合
{% include xxx.html %} 获取指定页面的所有内容
1.单独建个html存放编辑和添加页面得代码:
'''forms.HTML'''
编辑和添加页面
{ % include 'forms.html' %}
2.views.py
add
def add_view(self, request): # list_url="%s/%s/"%(self.app_name,self.model_name) DemoModelForm=self.get_model_form_class() #得到定义的ModelForm组件类 if request.method=='POST': form_obj=DemoModelForm(request.POST) if form_obj.is_valid(): form_obj.save() # return redirect(list_url) #不能用,相当于在当前路径后面拼接 return redirect(self.get_list_url()) else: return render(request,'add_view.html',locals()) form_obj = DemoModelForm() return render(request, 'add_view.html', locals())
edit
def change_view(self, request, id): DemoModelForm = self.get_model_form_class() # 得到定义的ModelForm组件类 change_obj=self.model.objects.get(pk=id) if request.method == 'POST': form_obj = DemoModelForm(request.POST,instance=change_obj) if form_obj.is_valid(): form_obj.save() # return redirect(list_url) #不能用,相当于在当前路径后面拼接 return redirect(self.get_list_url()) else: return render(request, 'change_view.html', locals()) form_obj = DemoModelForm(instance=change_obj) return render(request, 'change_view.html', locals())
del
def del_view(self, request, id): if request.method=='POST': self.model.objects.get(pk=id).delete() return redirect(self.get_list_url()) return render(request,"del_view.html",{ "url":self.get_list_url()})