新增出版社:

views:

#定义增加出版社功能:
def add_publisher(request):
if request.method == "POST":
#获取用户提交的数据:
pub_name = request.POST.get("pub_name")
#判断出版社名称是否已存在
if models.Publisher.objects.filter(name=pub_name):
return render(request,"add_publisher.html",{"error":"出版社名称已存在"})
#将数据插入到数据库中name名字等于输入的名字:
obj = models.Publisher.objects.create(name=pub_name)
print(obj.pk)
print(obj.name)
#跳转到展示页面:
return redirect("/publisher/")
return render(request,"add_publisher.html")
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><span>{{ error }}</span>
</p>

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

</body>
</html>

猜你喜欢

转载自www.cnblogs.com/zhang-da/p/12037167.html