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

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>作者列表</title>
</head>
<body> <h1>作者列表</h1> <table border="1">
<thead>
<tr>
<th>#</th>
<th>id</th>
<th>名字</th>
<th>书籍</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for author in author_list %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ author.id }}</td>
<td>{{ author.name }}</td>
<td>
{% for book in author.book.all %}
{% if forloop.last %}
{{ book.title }}
{% else %}
{{ book.title }} |
{% endif %}
{% endfor %}
</td> <td>
<a href="/del_author/?id={{ author.id }}">删除</a>
<a href="/edit_author/?id={{ author.id }}">编辑</a>
</td>
</tr>
{% endfor %}
</tbody>
</table> <a href="/add_author/">添加书籍</a> </body>
</html>

运行结果:

添加 edit_author.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>编辑作者</title>
</head>
<body> <h1>编辑作者</h1> <form action="/edit_author/" method="post">
<input type="text" name="author_id" value="{{ author.id }}" style="display: none">
<p>
作者姓名:<input type="text" name="author_name" value="{{ author.name }}">
</p> <p>
书籍:
<select multiple name="books">
{% for book in book_list %}
{% if book in author.book.all %}
<option selected value="{{ book.id }}">{{ book.title }}</option>
{% else %}
<option value="{{ book.id }}">{{ book.title }}</option>
{% endif %}
{% endfor %}
</select>
</p> <p>
<input type="submit" value="提交">
</p>
</form> </body>
</html>

解析:

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

from django.conf.urls import url
from django.contrib import admin
from app01 import views urlpatterns = [
# 出版社
url(r'^publisher_list/', views.publisher_list),
url(r'^add_publisher/', views.add_publisher),
url(r'^del_publisher/', views.del_publisher),
url(r'^edit_publisher/', views.edit_publisher),
# 书籍
url(r'^book_list/', views.book_list),
url(r'^add_book/', views.add_book),
url(r'^del_book/', views.del_book),
url(r'^edit_book/', views.edit_book),
# 作者
url(r'^author_list/', views.author_list),
url(r'^add_author/', views.add_author),
url(r'^del_author/', views.del_author),
url(r'^edit_author/', views.edit_author),
]

在 views.py 中添加 edit_author 函数

from django.shortcuts import render, redirect, HttpResponse
from app01 import models # 展示出版社列表
def publisher_list(request):
pass # 添加新的出版社
def add_publisher(request):
pass # 删除出版社
def del_publisher(request):
pass # 编辑出版社
def edit_publisher(request):
pass # 展示书籍列表
def book_list(request):
pass # 添加书籍
def add_book(request):
pass # 删除书籍
def del_book(request):
pass # 编辑书籍
def edit_book(request):
pass # 作者列表
def author_list(request):
# 查询所有作者
all_author = models.Author.objects.all()
return render(request, "author_list.html", {"author_list": all_author}) # 添加作者
def add_author(request):
if request.method == "POST":
# 取得提交的数据
new_author_name = request.POST.get("author_name")
# 如果提交的数据是多个值的话用 getlist
books = request.POST.getlist("books")
# 创建作者
new_author_obj = models.Author.objects.create(name=new_author_name)
# 把新作者和书籍建立对应关系,自动提交
new_author_obj.book.set(books)
# 跳转到作者列表页面,查看是否添加成功
return redirect("/author_list/")
# 查询所有的书籍
all_books = models.Book.objects.all()
return render(request, "add_author.html", {"book_list": all_books}) # 删除作者
def del_author(request):
# 从 url 里提取要删除的作者 id
del_id = request.GET.get("id")
# 根据 id 删除 author 表和其相关联表的数据
models.Author.objects.get(id=del_id).delete()
# 返回作者列表
return redirect("author_list.html") # 编辑作者
def edit_author(request):
# 从 post 提交过来的书籍
if request.method == "POST":
# 获取提交过来的作者 id 和姓名
edit_author_id = request.POST.get("author_id")
new_author_name = request.POST.get("author_name")
# 获取提交过来和作者相关联的书籍
new_book = request.POST.getlist("books")
# 根据 id 去数据库中查询当前编辑的作者对象
edit_author_obj = models.Author.objects.get(id=edit_author_id)
# 更新数据库中作者的名字
edit_author_obj.name = new_author_name
# 更新数据库中与作者关联的书籍的对应关系
edit_author_obj.book.set(new_book)
# 将修改保存到数据库中
edit_author_obj.save()
# 返回作者列表页面,查看编辑是否成功
return redirect("/author_list/") # 从 url 里获取要编辑作者的 id
edit_id = request.GET.get("id")
# 获取要编辑的作者对象
edit_author_obj = models.Author.objects.get(id=edit_id) # 获取对象书籍对象
all_book_list = models.Book.objects.all()
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. ArcGIS + Python 批量裁剪、添加X/Y坐标脚本

    前言 前一段时间,同事拿来的数据范围太大,用不了那么多(只需要一个乡镇的,结果拿来区县的),太多了加载也是问题.所以就让我给处理下. 由于文件较多,手动裁剪的话,我一个一个用ArcGIS工具箱中的工具 ...

  2. 《The one!》团队作业五:团队项目需求改进与系统设计

    项目 内容 作业所属课程 所属课程 作业要求 作业要求 团队名称 < The One !> 作业学习目标 (1)掌握面向对象需求分析方法:(2)学习软件系统总体结构和数据库逻辑结构设计,学 ...

  3. go语言-数据类型及类型之间转换

    数据类型分类 一.数据类型-基本数据类型 1.整数型(int.有符号(int8/1字节.int16/2字节.int32/4字节.int64/8字节).无符号(uint.uint8.uint16.uin ...

  4. [Dart] final vs const

    void main() { ; print(a); ; print(b); final c = 'Hello'; // c = 'Hello again'; // Uncomment to throw ...

  5. guava字符串工具 Splitter 主要功能是拆分字符串为集合 Map

    public class SplitterTest { public static void main(String args[]){ //1.拆分字符串为List集合 String str=&quo ...

  6. Devtool-Console

    1. console面板展示 1.全屏展示 打开开发者工具(option+cmd+i),点击console的tab 2. 在其他面板展示的同时展示console面板 1)esc快捷命令 2)或者选择打 ...

  7. 22 | MySQL有哪些“饮鸩止渴”提高性能的方法?

    不知道你在实际运维过程中有没有碰到这样的情景:业务高峰期,生产环境的MySQL压力太大,没法正常响应,需要短期内.临时性地提升一些性能. 我以前做业务护航的时候,就偶尔会碰上这种场景.用户的开发负责人 ...

  8. 三十四.MySQL主从同步 、主从同步模式

    mysql51:192.168.4.51 主 mysql52:192.168.4.52 从 mysql50:192.168.4.50 客户机   1.MySQL一主一从   1.1 51,52 安装m ...

  9. [转]引用模板类中定义的类型(用typedef或using)以及auto、decltype、typename的使用

    一.背景 使用typedef或者using定义类型别名是非常常见的手段,在c++里面,有时为了封装性,模块性等原因还会在某一个namespace或者class内部定义类型别名. 最近在写c++代码的时 ...

  10. NetworkX系列教程(9)-线性代数相关

    小书匠 Graph 图论  学过线性代数的都了解矩阵,在矩阵上的文章可做的很多,什么特征矩阵,单位矩阵等.grpah存储可以使用矩阵,比如graph的邻接矩阵,权重矩阵等,这节主要是在等到graph后 ...