django操作表单

viwes

def addbook(req):
    # b=Book(name='小王子', price=55, author='mistory',pub_data='2020-02-02')
    # b.save()
    Book.objects.create(name='王子', price=5, author='myself', pub_data='2020-2-02')
    # Book.objects.create(**dic)
    return HttpResponse('添加成功')


def update(req):
    #Book.objects.filter(name='小王子').update(price=123)
    b=Book.objects.get(name='王子')
    b.price=789
    b.save()
    #update是filter返回的组调用的方法
    return HttpResponse('修改成功')

def delete(req):
    Book.objects.filter(name='nicai').delete()
    return HttpResponse('删除成功')


def select(req):
    book_list=Book.objects.all()
    #book_list=Book.objects.all()[::-1]
    # book_list=Book.objects.get(id=2)#只能去除一条记录时才不报错
    ret=Book.objects.filter(author='wxq').values('name','price')
    #ret=Book.objects.filter(author='wxq').values_list('name','price')#列表类型
    print(ret)
    #bool_list=Book.objects.fileter(price__gt=50).values(name','price')#价格大于50
                                    #(name_contain='p')#包含
    return render(req,'index.html',{'book_list':book_list})

html


<body>
<div class="outer">
    <div class="head">标题</div>
    <div class="content"></div>
        <a href="/addbook/">添加</a>
        <a href="/update/">修改</a>
        <a href="/delete/">删除</a>
        <a href="/select/">查询</a>
</div>
<hr>
<div class="queryresult">
    {% for book in book_list%}
        <div>
        <P>{{ book.name }}{{ book.author }}{{ book.price }}</P>
        </div>
    {% endfor %}
</div>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值