获取表单信息并添加

前端代码如下:

<form  action="/addbook/" method="post">
书本名称:<input type="text" name="name"><br/>
书本价格:<input type="text" name="price"/><br/>
更新日期:<input type="date" name="pub_date"><br/>
作者:<input type="text" name="author"/><br/>
<input type="submit" value="添加书本信息"/>
</form>
addbook方法用来出来表单添加,代码如下:
def  addbook(request):
if request.method=="POST":
name=request.POST.get("name")
price=request.POST.get("price")
pub_date=request.POST.get("pub_date")
author=request.POST.get("author")
Book.objects.create(name=name,price=price,author=author,pub_date=pub_date)
#全部添加Book.objects.create(**dic)
return HttpResponse("添加成功")
运行以后出现下面的提示:

在页面中添加下面的代码用于处理这个问题:

再次运行提示添加成功,查看数据库,确认数据已经添加


原文地址:https://www.cnblogs.com/woshinige/p/9968966.html