首页 > 其他 > 详细

编辑出版社:

时间:2019-12-13 22:01:44      阅读:84      评论:0      收藏:0      [点我收藏+]

views:

#定义编辑功能:
def edit_publisher(request):
#获取要编辑对象的id:
pk = request.GET.get("id")
obj = models.Publisher.objects.filter(pk = pk).first()
#判断如果不是真的话返回要展示的页面:
if not obj:
return HttpResponse("所查询的数据不存在")
if request.method == "POST":
pub_name = request.POST.get("pub_name")
# obj.name = pub_name #内存中修改了属性
# obj.save() #提交保存
if models.Publisher.objects.filter(name=pub_name):
return render(request,"edit_publisher.html",{{"error":"出版社名称已存在"}})
models.Publisher.objects.filter(pk = pk).update(name=pub_name)
#重定向到展示页面:
return redirect("/publisher/")
return render(request,"edit_publisher.html",{"obj":obj})

HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>

<form action="" method="post">
<p>
出版社名称:<input type="text" name="pub_name" required value="{{ obj.name }}"><span>{{ error }}</span>
</p>

<button>提交</button>
</form>

</body>
</html>

编辑出版社:

原文:https://www.cnblogs.com/zhang-da/p/12037395.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!