zoukankan      html  css  js  c++  java
  • django-orm更新数据

    django-orm更新数据

    单个数据修改(更新单个数据)

    In [6]: b1=Book.objects.get(id=1)
    In [7]: b1.price
    Out[7]: Decimal('20.00')
    In [8]: b1.price=22
    
    mysql> select * from book where id=1;
    +----+--------+-------+--------------+-----------------------+
    | id | title  | price | market_price | pub                   |
    +----+--------+-------+--------------+-----------------------+
    |  1 | python | 20.00 |        25.00 | 清华大学出版社         |
    +----+--------+-------+--------------+-----------------------+
    1 row in set (0.00 sec)
    
    In [9]: b1.save()
    
    mysql> select * from book where id=1;
    +----+--------+-------+--------------+-----------------------+
    | id | title  | price | market_price | pub                   |
    +----+--------+-------+--------------+-----------------------+
    |  1 | python | 22.00 |        25.00 | 清华大学出版社         |
    +----+--------+-------+--------------+-----------------------+
    1 row in set (0.00 sec)
    
    

    批量数据更新

    In [10]: b2=Book.objects.filter(pub='清华大学出版社')
    In [11]: b2.update(price=1)
    Out[11]: 3
    In [12]: for i in b2:
        ...:     print(i.price)
        ...:
    1.00
    1.00
    1.00
    

    image

    views.py

    def update(request,book_id):
        try:
            book=Book.objects.get(id=book_id)
        except Exception as e:
            print('---update book error is %s----'%(e))
            return HttpResponse('更新书籍不存在')
        if request.method=='GET':
            id=book.id
            title=book.title
            pub=book.pub
            price=book.price
            market_price=book.market_price
        
            return render(request,'bookstore/update.html',locals())
    
    
        elif request.method=='POST':
            id=book.id
            update_book=Book.objects.get(id=id)
            price=request.POST.get('price')
            market_price=request.POST.get('market_price')
            update_book.price=price
            update_book.market_price=market_price
            update_book.save()
            url=reverse('all_book')
            return HttpResponseRedirect(url)
            # return HttpResponseRedirect('/bookstore/all_book')
    
    
    

    all_book.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>显示所有书籍</title>
    </head>
    <body>
        <table border="1">
            <tr>
                <th>id</th>
                <th>title</th>
                <th>pu</th>
                <th>price</th>
                <th>market_price</th>
                <th>op</th>
            </tr>
            {%  for info in  all_book %}
            <tr>
                 <td>{{info.id}}</td>
                 <td>{{info.title}}</td>
                <td>{{info.pub}}</td>
                <td>{{info.price}}</td>
    
                <td>{{info.market_price}}</td>
                <td>
                    <a href="{% url 'update' info.id %}">更新</a>
                    <a href="">删除</a>
                </td>
            </tr>
            {% endfor %}
        </table>
    </body>
    </html>
    

    update.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>更新数据</title>
      
    </head>
    <body>
        <form action="{% url 'update' id %}" method="POST">
            {% csrf_token%}
         
            <span>title</span><input type="text"  value={{title}} disabled='disabled'> <br>
            <span>pub</span><input type="text"  value={{pub}} disabled='disabled'> <br>
            <span>price</span><input type="text" name="price" value={{price}}> <br>
            <span>market_price</span><input type="text" name="market_price" value={{market_price}}> <br>
            <input type="submit" value="更新">
        </form>
        
    </body>
    </html>
    

    urls.py

    from django.contrib import admin
    from django.urls import path,include
    
    from . import views
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('showimg/',views.showimg),
    
        path('music/',include('music.urls')),
        path('bookstore/',include('bookstore.urls'))
    ]
    

    页面显示
    image
    image

  • 相关阅读:
    JSOUP 简单爬取网页
    RSS 实体清单
    [转载]从零开始学习OpenGL ES之四 – 光效
    [转载]从零开始学习OpenGL ES之四补遗 – setupView重写
    [转载]Xcode创建的默认iOS OpenGL ES 2.0 project代码分析
    [转载]从零开始学习OpenGL ES之八 – 交叉存取顶点数据
    [转载]从零开始学习OpenGL ES之十 – 完结
    [转载]从零开始学习OpenGL ES之五 – 材质
    [转载]从零开始学习OpenGL ES之六 – 纹理及纹理映射
    [转载]从零开始学习OpenGL ES之九(上) – 动画基础和关键帧动画
  • 原文地址:https://www.cnblogs.com/yescarf/p/15122080.html
Copyright © 2011-2022 走看看