5django数据库-增删改查

6aea86a8260a08fef1837937d3acfc0790f.jpg

开局 运行-配置数据库

python manage.py makemigrations
python manage.py migrate

res=Book.objects.get(bid=id)  结果出来是对象
Book.objects.filter(bid=id).delete() 结果出来是列表+对象
res=Book.objects.exclude(bid=id)  结果出来是列表+对象
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form method="post" action="/bookd/">
    {% csrf_token %}
    <input type="text" name="bname"><br>
    <button type="submit">添加书本</button>
</form>
<p> ***********************************</p>

<form method="post" action="/chaxun/">
{% csrf_token %}
<button type="submit" >查询全部</button>
{% for ii in booklist %}

    <br>{{ ii.bid }}
    {{ ii.bname }}<br>

{% endfor %}
</form>
<p> ***********************************</p>

<form method="post" action="/chaxun2/">
{% csrf_token %}
id:<input type="text" name="bid"><br>
    <button type="submit">条件查询</button>

    <br>{{  book.bid }}
    {{  book.bname }}<br>
</form>

<p> ***********************************</p>

<form method="post" action="/shanchu/">
{% csrf_token %}
ID:<input type="text" name="bid"><br>
    <button type="submit">删除</button>

</form>

<p> ***********************************</p>

<form method="post" action="/xiugai/">
{% csrf_token %}
ID:<input type="text" name="bid"><br>
   名字: <input type="text" name="name"><br>
    <button type="submit">修改名字</button>

</form>



</body>
</html>

5221b29041c88db8a099da2824110efbaa4.jpg

116511eb60fe64580e9516acd5f4b912352.jpg

def bookd(request):
    if request.POST:
        name=request.POST.get('bname')
        book=Book(bname=name)
        book.save()
        return HttpResponse("<script>alert('成功')</script>")
    else:
        return render(request,'bookd.html')
def chaxun(request):
    if request.POST:
        res=Book.objects.all()
        print(res)
        content={}
        content["booklist"]=res
        return render(request,'bookd.html',content)
    else:
        return render(request,'bookd.html')

def chaxun2(request):
    if request.POST:
        id=request.POST.get('bid')
        res=Book.objects.get(bid=id)
        content2 = {}
        content2["book"] = res
        return render(request, 'bookd.html', content2)
    else:
        return render(request,'bookd.html')

def shanchu(request):
    if request.POST:
        id=request.POST.get('bid')
        Book.objects.filter(bid=id).delete()

        return HttpResponse("<script>alert('删除成功')</script>")
    else:
        return render(request,'bookd.html')

def xiugai(request):
    if request.POST:
        id=request.POST.get('bid')
        name = request.POST.get('name')
        Book.objects.filter(bid=id).update(bname=name)

        return HttpResponse("<script>alert('修改成功')</script>")
    else:
        return render(request,'bookd.html')

a296d285d625cb93fc1b89a87f9025cda87.jpg

def chaxun3(request):
    if request.POST:
        id=request.POST.get('bid')
        res=Book.objects.exclude(bid=id)
        print(res)
        content2 = {}
        content2["booklists"] = res
        return render(request, 'bookd.html', content2)
    else:
        return render(request,'bookd.html')

 

转载于:https://my.oschina.net/u/4036782/blog/2963110

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值