在作者列表页面的操作栏中加上编辑按钮

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>作者列表</title>
  6. </head>
  7. <body>
  8.  
  9. <h1>作者列表</h1>
  10.  
  11. <table border="1">
  12. <thead>
  13. <tr>
  14. <th>#</th>
  15. <th>id</th>
  16. <th>名字</th>
  17. <th>书籍</th>
  18. <th>操作</th>
  19. </tr>
  20. </thead>
  21. <tbody>
  22. {% for author in author_list %}
  23. <tr>
  24. <td>{{ forloop.counter }}</td>
  25. <td>{{ author.id }}</td>
  26. <td>{{ author.name }}</td>
  27. <td>
  28. {% for book in author.book.all %}
  29. {% if forloop.last %}
  30. {{ book.title }}
  31. {% else %}
  32. {{ book.title }} |
  33. {% endif %}
  34. {% endfor %}
  35. </td>
  36.  
  37. <td>
  38. <a href="/del_author/?id={{ author.id }}">删除</a>
  39. <a href="/edit_author/?id={{ author.id }}">编辑</a>
  40. </td>
  41. </tr>
  42. {% endfor %}
  43. </tbody>
  44. </table>
  45.  
  46. <a href="/add_author/">添加书籍</a>
  47.  
  48. </body>
  49. </html>

运行结果:

添加 edit_author.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>编辑作者</title>
  6. </head>
  7. <body>
  8.  
  9. <h1>编辑作者</h1>
  10.  
  11. <form action="/edit_author/" method="post">
  12. <input type="text" name="author_id" value="{{ author.id }}" style="display: none">
  13. <p>
  14. 作者姓名:<input type="text" name="author_name" value="{{ author.name }}">
  15. </p>
  16.  
  17. <p>
  18. 书籍:
  19. <select multiple name="books">
  20. {% for book in book_list %}
  21. {% if book in author.book.all %}
  22. <option selected value="{{ book.id }}">{{ book.title }}</option>
  23. {% else %}
  24. <option value="{{ book.id }}">{{ book.title }}</option>
  25. {% endif %}
  26. {% endfor %}
  27. </select>
  28. </p>
  29.  
  30. <p>
  31. <input type="submit" value="提交">
  32. </p>
  33. </form>
  34.  
  35. </body>
  36. </html>

解析:

在 urls.py 中添加 edit_author 的对应关系

  1. from django.conf.urls import url
  2. from django.contrib import admin
  3. from app01 import views
  4.  
  5. urlpatterns = [
  6. # 出版社
  7. url(r'^publisher_list/', views.publisher_list),
  8. url(r'^add_publisher/', views.add_publisher),
  9. url(r'^del_publisher/', views.del_publisher),
  10. url(r'^edit_publisher/', views.edit_publisher),
  11. # 书籍
  12. url(r'^book_list/', views.book_list),
  13. url(r'^add_book/', views.add_book),
  14. url(r'^del_book/', views.del_book),
  15. url(r'^edit_book/', views.edit_book),
  16. # 作者
  17. url(r'^author_list/', views.author_list),
  18. url(r'^add_author/', views.add_author),
  19. url(r'^del_author/', views.del_author),
  20. url(r'^edit_author/', views.edit_author),
  21. ]

在 views.py 中添加 edit_author 函数

  1. from django.shortcuts import render, redirect, HttpResponse
  2. from app01 import models
  3.  
  4. # 展示出版社列表
  5. def publisher_list(request):
  6. pass
  7.  
  8. # 添加新的出版社
  9. def add_publisher(request):
  10. pass
  11.  
  12. # 删除出版社
  13. def del_publisher(request):
  14. pass
  15.  
  16. # 编辑出版社
  17. def edit_publisher(request):
  18. pass
  19.  
  20. # 展示书籍列表
  21. def book_list(request):
  22. pass
  23.  
  24. # 添加书籍
  25. def add_book(request):
  26. pass
  27.  
  28. # 删除书籍
  29. def del_book(request):
  30. pass
  31.  
  32. # 编辑书籍
  33. def edit_book(request):
  34. pass
  35.  
  36. # 作者列表
  37. def author_list(request):
  38. # 查询所有作者
  39. all_author = models.Author.objects.all()
  40. return render(request, "author_list.html", {"author_list": all_author})
  41.  
  42. # 添加作者
  43. def add_author(request):
  44. if request.method == "POST":
  45. # 取得提交的数据
  46. new_author_name = request.POST.get("author_name")
  47. # 如果提交的数据是多个值的话用 getlist
  48. books = request.POST.getlist("books")
  49. # 创建作者
  50. new_author_obj = models.Author.objects.create(name=new_author_name)
  51. # 把新作者和书籍建立对应关系,自动提交
  52. new_author_obj.book.set(books)
  53. # 跳转到作者列表页面,查看是否添加成功
  54. return redirect("/author_list/")
  55. # 查询所有的书籍
  56. all_books = models.Book.objects.all()
  57. return render(request, "add_author.html", {"book_list": all_books})
  58.  
  59. # 删除作者
  60. def del_author(request):
  61. # 从 url 里提取要删除的作者 id
  62. del_id = request.GET.get("id")
  63. # 根据 id 删除 author 表和其相关联表的数据
  64. models.Author.objects.get(id=del_id).delete()
  65. # 返回作者列表
  66. return redirect("author_list.html")
  67.  
  68. # 编辑作者
  69. def edit_author(request):
  70. # 从 post 提交过来的书籍
  71. if request.method == "POST":
  72. # 获取提交过来的作者 id 和姓名
  73. edit_author_id = request.POST.get("author_id")
  74. new_author_name = request.POST.get("author_name")
  75. # 获取提交过来和作者相关联的书籍
  76. new_book = request.POST.getlist("books")
  77. # 根据 id 去数据库中查询当前编辑的作者对象
  78. edit_author_obj = models.Author.objects.get(id=edit_author_id)
  79. # 更新数据库中作者的名字
  80. edit_author_obj.name = new_author_name
  81. # 更新数据库中与作者关联的书籍的对应关系
  82. edit_author_obj.book.set(new_book)
  83. # 将修改保存到数据库中
  84. edit_author_obj.save()
  85. # 返回作者列表页面,查看编辑是否成功
  86. return redirect("/author_list/")
  87.  
  88. # 从 url 里获取要编辑作者的 id
  89. edit_id = request.GET.get("id")
  90. # 获取要编辑的作者对象
  91. edit_author_obj = models.Author.objects.get(id=edit_id)
  92.  
  93. # 获取对象书籍对象
  94. all_book_list = models.Book.objects.all()
  95. return render(request, "edit_author.html", {"book_list": all_book_list, "author": edit_author_obj})

运行效果:

编辑 John

《Java》和《C》默认是被选择的,将 《Python》也选上

提交后

来数据库中看一下

作者 id 为 1 的关联书籍多了一个 2

Python - Django - 编辑作者的更多相关文章

  1. Python - Django - 删除作者

    修改 author_list.html,添加删除按钮 <!DOCTYPE html> <html lang="en"> <head> <m ...

  2. Python - Django - 添加作者

    在 book_list.html 的页面下方加上 “添加作者” 的链接 <!DOCTYPE html> <html lang="en"> <head& ...

  3. Python - Django - 显示作者列表

    在 views.py 中添加展示作者列表的函数 from django.shortcuts import render, redirect, HttpResponse from app01 impor ...

  4. python Django教程 之 模型(数据库)、自定义Field、数据表更改、QuerySet API

    python  Django教程  之 模型(数据库).自定义Field.数据表更改.QuerySet API 一.Django 模型(数据库) Django 模型是与数据库相关的,与数据库相关的代码 ...

  5. Python+Django+Eclipse 在Windows下快速开发自己的网站

    一.配置开发环境 我的开发环境是:Python3.3.2 + Django1.5.2 + Eclipse 1.安装Python 下载地址:http://www.python.org/getit/ 安装 ...

  6. python Django 学习笔记(一)—— Django安装

    注:本人python版本2.7.5 ,win7系统 安装Django https://www.djangoproject.com/download/ 官方下载Django-1.5.5.tar.gz 1 ...

  7. 教你如何将 Sublime 3 打造成 Python/Django IDE开发利器

    Sublime Text 是一款非常强大的文本编辑器, 下面我们介绍如何将 Sublime Text 3 打造成一款 Python/Django 开发利器: 1. 安装 Sublime Text 3 ...

  8. 将 Sublime 3 打造成 Python/Django IDE

    Sublime Text 是一款非常强大的文本编辑器, 下面我们介绍如何将 Sublime Text 3 打造成一款 Python/Django 开发利器: 1. 安装 Sublime Text 3 ...

  9. Python+Django+SAE系列教程17-----authauth (认证与授权)系统1

    通过session,我们能够在多次浏览器请求中保持数据,接下来的部分就是用session来处理用户登录了. 当然,不能仅凭用户的一面之词,我们就相信,所以我们须要认证. 当然了,Django 也提供了 ...

随机推荐

  1. P1880 [NOI1995]石子合并[环形DP]

    题目来源:洛谷 题目描述 在一个圆形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分. 试设计出1个算法,计算出将 ...

  2. JS多线程之Web Worker

    什么是Web Worker web worker 是运行在后台的 JavaScript,不会影响页面的性能. 当在 HTML 页面中执行脚本时,页面的状态是不可响应的,直到脚本已完成. web wor ...

  3. Mysql DELETE 不能使用别名? 是我不会用!

    今天碰到一个sql问题,就是在delete中加了别名,导致报错了:"[Err] 1064 - You have an error in your SQL syntax; ..." ...

  4. C++——构造和析构函数

    现在学习进入第三阶段,对c++要有更深入的学习,关于构造函数和析构函数这一块需要总结一下,来深刻理解这两个函数的意义. 什么是构造函数和析构函数呢呢?听着就很高大上,但是要从心里藐视它.就像自然万物有 ...

  5. 为RIDE创建桌面快捷方式

    问题场景:默认情况下,RIDE的图标不是自动创建的,需要手动添加.     解决方法: 在桌面上新建"快捷方式" 目标对象的位置:C:\Python27\python2.exe - ...

  6. web-----chrome DevTools工具的常用使用记录

     注:1.Chrome浏览器,2.在浏览器页面 右键--->检查,即可看到此页面 跟开发沟通过.他们查看一个页面的性能一般都会使用”检查“来观测.查找对应的数据记录. 设置网络的网速和通过设置某 ...

  7. ADB命令使用详解

    ADB是一个 客户端-服务器端 程序, 其中客户端是你用来操作的电脑, 服务器端是android设备. 1.连接android设置 adb connect 设备名 例如: adb connect 12 ...

  8. AcWing P373 車的放置

    Analysis 这道题是二分图匹配,设可以放車的的地方的坐标为(i,j),则连一条i到j的有向边(注意是有向边),然后再跑匈牙利算法就好了.时间复杂度是O(nm(n+m)),在1≤n,m≤200的情 ...

  9. hdu 3 * problem

    hdu 6182 给出 $n$ 求 $\sum_{i = 1} ^ {\infty} (i * i <= n)$ 暴力枚举 hdu 6186 给出 $n$ 个数 $1e6$ 次询问,每次询问这 ...

  10. 【知识点】Java常用类库

    1.字符串 修改字符串内容用StringBuffer,没有“+”,需要用append(),否则用String 2.JVM 相关 Runtime,单例模式,通过getRuntime()获取实例,可以调用 ...