django 表单编辑功能的实现   发布时间:2017-09-12 15:32:33

django 表单编辑功能的实现

def edit_server(request, eid): sid = int(eid) edit_svr = machine.objects.get(id=sid) if request.method == 'POST': edit_form = ServerForm(request.POST, instance=edit_svr) if edit_form.is_valid(): edit_form.save() return HttpResponseRedirect('/server/assets') else: edit_form = ServerForm(instance=edit_svr) return render_to_response('edit.html', {'edit_form': edit_form, 'sid': sid})

2.编辑urls.py,加入如下语句:

url(r'^edit/(?P<eid>\d+)/$', 'server.views.edit_server'),

3.edit.html

<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>Edit</title> <script LANGUAGE="JavaScript"> function del(){ if(window.confirm("确实要删除吗?")){ window.location ="/server/del/"; }else{ return; } } </script> </head> <body> <form action="" method="post"> {{ edit_form.as_p }} <input type="submit" value="保存"> </form> </body> </html>

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:武汉建网站 http://www.45qun.com