首页 编程教程正文

django的ORM操作 删除和编辑实现详解

piaodoo 编程教程 2020-02-02 11:58:20 985 0 python教程

这篇文章主要介绍了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 }}" rel="external nofollow" rel="external nofollow" ><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/' %}" rel="external nofollow" >

  <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/" rel="external nofollow" ><button class="btn btn-primary">添加</button></a>
{#             在get请求时,url加上删除时点击到的id,获取id,就可以删除#}
             <a href="/delbook/?id={{ book.id }}" rel="external nofollow" rel="external nofollow" ><button class="btn btn-primary">删除</button></a>
{#             取到路径,#}
            <a href="/editorbook/{{ book.id }}" rel="external nofollow" ><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/' %} " rel="external nofollow" >

  <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})

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

版权声明:

本站所有资源均为站长或网友整理自互联网或站长购买自互联网,站长无法分辨资源版权出自何处,所以不承担任何版权以及其他问题带来的法律责任,如有侵权或者其他问题请联系站长删除!站长QQ754403226 谢谢。

有关影视版权:本站只供百度云网盘资源,版权均属于影片公司所有,请在下载后24小时删除,切勿用于商业用途。本站所有资源信息均从互联网搜索而来,本站不对显示的内容承担责任,如您认为本站页面信息侵犯了您的权益,请附上版权证明邮件告知【754403226@qq.com】,在收到邮件后72小时内删除。本文链接:http://www.piaodoo.com/652.html

评论

搜索