zoukankan      html  css  js  c++  java
  • django的ORM操作 删除和编辑

    向server端传送数据,
    有2中方法,1 是 通过url 地址, 2 是通过路径

    #向server端传参数方式 #1,通过数据 http://127.0.0.1:8000/blog/?id=2 #2, 通过路径 http://17.0.0.1:8000/blog/20 # url(r'blog/(d{4})')

    删除功能:

    在url文件中,创建一个delbook路径, 通过url的地址拿到id实现删除

    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^$',views.index),#指定一个根目录,也指向index页面
        url(r'^index/$',views.index),
        url(r'^addbook/$',views.addbook),
        url(r'^delbook/$',views.delbook),  -------------------------------------------del删除功能,对应视图函数
        #(d+)分组后,作为参数传给editorbook函数,editorbook(request,1或2 等等)
        url(r'^editorbook/(d+)',views.editorbook),
    ]

    在index.html 页面中,点击删除按钮,在href 加上?id={{ book.id}}要删除的书籍,

      在get请求时,url加上删除时点击到的id,获取id,就可以删除#}
      <a href="/delbook/?id={{ book.id }}"><button class="btn btn-primary">删除</button></a>

     在删除一条记录后,页面的顺序是错乱,在前端显示的是数据库的id,用forloop.counter 默认从1开始循环显示,与数据库的id无关,

      <td>{{ forloop.counter }}</td>

    {% load staticfiles %}
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link rel="stylesheet" href="{% static '/bootstrap-3.3.7/dist/css/bootstrap.css/' %}">
    
        <style>
    
            .container{
                margin-top: 50px;
            }
    
        </style>
    
    </head>
    <body>
    
    
    <div class="container">
        <div class="row">
    
    
            <div class="col-md-8 col-md-offset-2">
    
            <table class="table table-striped">
                <tr>
                    <th>ID</th>
                    <th>书名</th>
                    <th>价格</th>
                    <th>出版日期</th>
                    <th>作者</th>
                    <th>出版社</th>
                    <th>分类</th>
                    <th>操作</th>
                </tr>
    
                 {% for book in book_list %}
                <tr>
    
    {# 在前端显示的是数据库的id,用forloop.counter 默认从1开始循环显示,与数据库的id无关,     <td>{{ book.id }}</td>#}
                        <td>{{ forloop.counter }}</td>--------------------按照顺序显示,
                        <td>{{ book.name }}</td>
                        <td>{{ book.price }}</td>
                        <td>{{ book.Date }}</td>
                        <td>{{ book.auth }}</td>
                        <td>{{ book.publish }}</td>
                        <td>{{ book.classification }}</td>
                        <td>
    {#                        当前的ip和端口都可以省略,会自动添加,a标签会访问addbook路径#}
                             <a href="/addbook/"><button class="btn btn-primary">添加</button></a>
    {#                         在get请求时,url加上删除时点击到的id,获取id,就可以删除#}
                             <a href="/delbook/?id={{ book.id }}"><button class="btn btn-primary">删除</button></a>
    {#                         取到路径,#}
                            <a href="/editorbook/{{ book.id }}"><button class="btn btn-primary">编辑</button></a>
                        </td>d
    
                </tr>
                {% endfor %}
            </table>
    
        </div>
        </div>
    
    </div>
    </body>
    
    <script>
    
    
    </script>
    
    </html>

    在views文件中,编辑delbook函数,

    django里的删除和编辑,前提都是 要先找到,利用filter()方法,条件是id或者是name,等都可以,

    步骤1,用get的方法,从url路径中拿到id,

    步骤2,对数据库的id和要url里获取到的id,对应,就执行delete()方法,就删除指定的记录,数据库也会减少一条记录,

    #删除和修改,都是要先找到记录(对象)
    def delbook(request):
    
        #先过滤,加上过滤的条件,然后用delete()
        #向server端传参数方式
        #1,通过数据 http://127.0.0.1:8000/blog/?id=2
        #2, 通过路径  http://17.0.0.1:8000/blog/20
                            # url(r'blog/(d{4})')
        #在前端页面加上id值,{{book.id}}
    
        #通过url获取iD,是get的方法,"id"是url里的key,
        id = request.GET.get("id")
        #前面的id是表里的字段,把get从地址栏里获取到的id赋值给表里的id,就可以删除
        #
        Book.objects.filter(id = id).delete()
    
        return redirect('/index/')

    =======

    ORM的编辑功能

    在url文件中创建editorbook路径,和映射到视图函数,

    通过访问的路径,拿到id,

    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^$',views.index),#指定一个根目录,也指向index页面
        url(r'^index/$',views.index),
        url(r'^addbook/$',views.addbook),
        url(r'^delbook/$',views.delbook),  -------------------------------------------del删除功能,对应视图函数
        #(d+)分组后,作为参数传给editorbook函数,editorbook(request,1或2 等等)
        url(r'^editorbook/(d+)',views.editorbook),-----------------------------editorbook  编辑功能,对应一个视图函数
    ]

    编辑一个editorbook页面,

    编辑时要获取要编辑的是哪个对象,

    比如:blog/20 ,20就是要获取到的id

    通过路径 http://17.0.0.1:8000/blog/20

      # url(r'blog/(d{4})')

    {% load staticfiles %}
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link rel="stylesheet" href="{%  static '/bootstrap-3.3.7/dist/css/bootstrap.css/' %} ">
    
        <style>
            .container{
                margin-top: 50px;
            }
        </style>
    
    </head>
    <body>
    
    
    <div class="container">
    
        <div class="row">
    
            <div  class="col-md-6 col-md-offset-2">
    {#            {{ csrf-token }}#}
    
    {#            以post的方法提交数据,走到editbook 视图函数#}
                <form  class=editorbook" action="/editorbook/{{ book_obj.id }}/" method="post">  -----------加上从数据库里拿到的id号,
    
    {#            当点击添加按钮时,是执行视图函数,在数据库中添加一条记录,然后再把这个记录添加到index页面#}
    
                <div  class="form-group">
                    <label for="bookname">书名:</label>
                    <input type="text" class="form-control" id="bookname" name="bookname" value="{{ book_obj.name }}">
                </div>
                <div  class="form-group">
                    <label for="price">价格:</label>
                    <input type="text" class="form-control" id="price" name="price" value="{{ book_obj.price }}">
                </div>
                <div  class="form-group">
                    <label for="Date">日期:</label>
                    <input type="text" class="form-control" id="Date" name="Date" value="{{ book_obj.Date }}">
                </div>
                <div  class="form-group">
                    <label for="auth">作者:</label>
                    <input type="text" class="form-control" id="auth" name="auth" value="{{ book_obj.auth }}">
                </div>
    
                <div  class="form-group">
                    <label for="publish">出版社:</label>
                    <input type="text" class="form-control" id="publish" name="publish" value="{{ book_obj.publish }}">
                </div>
    
                <div  class="form-group">
                    <label for="publish">分类:</label>
                    <input type="text" class="form-control" id="publish" name="classification" value="{{ book_obj.classification }}">
                </div>
    
                <input class="btn btn-info" type="submit" value='提交'>
    
                </form>
            </div>
        </div>
    
    </div>
    
    
    </body>
    
    </html>

    在views文件中,撰写editorbook函数,

    表单的提交是post 方法,用post的方法获取到每个输入框的值,  然后保存到数据库

    def editorbook(request,id):
    
    # 2, 通过路径  http://17.0.0.1:8000/blog/20
    # url(r'blog/(d{4})')
    #通过id获取要修改的对象,在前端中把对象的每个字段属性给value,就可以在input框显示要编辑的值
        # book_obj = Book.objects.filter(id = id)[0]
        book_obj = Book.objects.filter(id = id).first()
    

    if request.method == "POST": bookname = request.POST.get('bookname') price = request.POST.get('price') Date = request.POST.get('Date') auth = request.POST.get('auth') publish = request.POST.get('publish') classification = request.POST.get('classification') #方法2 update修改并保存数据 ,name 是数据库的字段,bookname是前端form表单里 的name属性值,把输入框里获取到的值给数据库的字段进行保存, Book.objects.filter(id = id).update(name = bookname,price = price, Date = Date, auth = auth , publish = publish, classification = classification) return redirect('/index/') return render(request,'editorbook.html',{'book_obj':book_obj})
    def editorbook(request,id):

    # 2, 通过路径 http://17.0.0.1:8000/blog/20
    # url(r'blog/(d{4})')
    #通过id获取要修改的对象,在前端中把对象的每个字段属性给value,就可以在input框显示要编辑的值
    # book_obj = Book.objects.filter(id = id)[0]
    book_obj = Book.objects.filter(id = id).first()

    if request.method == "POST":
    bookname = request.POST.get('bookname')
    price = request.POST.get('price')
    Date = request.POST.get('Date')
    auth = request.POST.get('auth')
    publish = request.POST.get('publish')
    classification = request.POST.get('classification')


    #方法2 update修改并保存数据
    Book.objects.filter(id = id).update(name = bookname,price = price, Date = Date, auth = auth , publish = publish, classification = classification)

    return redirect('/index/')

    return render(request,'editorbook.html',{'book_obj':book_obj})
  • 相关阅读:
    Tkinter之Label部件
    Tkinter编码风格
    GUI之tkinter视窗设计模块
    绘制函数图像
    backbone学习总结(一)
    实习两个月,写在辞职的今天
    Spark SQL 编程初级实践2- 编程实现利用 DataFrame 读写 MySQL 的数据
    Spark SQL 编程初级实践1-Spark SQL 基本操作
    spark创建DataFrame的几种方式
    python-with open() as file相关参数以及常用打开方式
  • 原文地址:https://www.cnblogs.com/gyh04541/p/7904298.html
Copyright © 2011-2022 走看看