在 book_list.html 的页面下方加上 “添加作者” 的链接

<!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>
</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>
</tr>
{% endfor %}
</tbody>
</table> <a href="/add_author/">添加书籍</a> </body>
</html>

运行效果:

在 urls.py 处添加 “添加作者” 的 url 对应关系

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),
]

创建 add_author.html 添加作者页面

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加作者</title>
</head>
<body> <h1>添加作者</h1> <form action="/add_author/" method="post">
<p>
作者姓名:<input type="text" name="author_name">
</p> <p>
书籍:
<select multiple name="books">
{% for book in book_list %}
<option value="{{ book.id }}">{{ book.title }}</option>
{% endfor %}
</select>
</p> <p>
<input type="submit" value="提交">
</p>
</form> </body>
</html>

最后在 views.py 加上添加作者的函数

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

运行结果:

提交后

再看一下数据库

Python - Django - 添加作者的更多相关文章

  1. Python - Django - 编辑作者

    在作者列表页面的操作栏中加上编辑按钮 <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  2. Python - Django - 删除作者

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

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

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

  4. Python - Django - 添加首页尾页上一页下一页

    添加首页和尾页: views.py: from django.shortcuts import render from app01 import models def book_list(reques ...

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

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

  6. python——django使用mysql数据库(一)

    之前已经写过如何创建一个django项目,现在我们已经有了一个小骷髅,要想这个web工程变成一个有血有肉的人,我们还需要做很多操作.现在就先来介绍如何在django中使用mysql数据库. 前提:已经 ...

  7. python Django教程 之模板渲染、循环、条件判断、常用的标签、过滤器

    python3.5 manage.py runserver python Django教程 之模板渲染.循环.条件判断.常用的标签.过滤器 一.Django模板渲染模板 1. 创建一个 zqxt_tm ...

  8. python Django教程 之 安装、基本命令、视图与网站

    python  Django教程  之 安装.基本命令.视图与网站 一.简介 Django 中提供了开发网站经常用到的模块,常见的代码都为你写好了,通过减少重复的代码,Django 使你能够专注于 w ...

  9. python django 多级业务树形结构规划及页面渲染

    概述: 在项目中,父级到子级结构并不少见,如果仅仅的两层树形结构,我们可以使用数据库的外键设计轻松做到,子级业务表设计一字段外键到父级业务表,这样子到父.父到子的查询都非常简单. 但是往往父子结构会有 ...

随机推荐

  1. addEventListener和attachEvent的区别(转载)

    attachEvent 与addEventListener到底有什么区别呢?总结如下: 一.适应的浏览器版本不同 attachEvent——兼容:IE7.IE8:不兼容firefox.chrome.I ...

  2. centos 下 sphinx安装和配置

    一.安装前提必备先安装工具 yum -y install make gcc g++ gcc-c++ libtool autoconf automake imake mysql-devel libxml ...

  3. 多任务3(协程)--yield完成多任务交替执行

    协程是并发,单线程,一次执行一个 来回切换 代码: import time def task_1(): while True: print("-----1-----") time. ...

  4. Educational Codeforces Round 75 (Rated for Div. 2) D. Salary Changing

    链接: https://codeforces.com/contest/1251/problem/D 题意: You are the head of a large enterprise. n peop ...

  5. sping boot 笔记

    参考  http://blog.csdn.net/catoop/article/details/50501664# 一.简介 Spring 官方网站本身使用Spring 框架开发,随着功能以及业务逻辑 ...

  6. wangEditor编辑器从word粘贴公式

    图片的复制无非有两种方法,一种是图片直接上传到服务器,另外一种转换成二进制流的base64码目前限chrome浏览器使用首先以um-editor的二进制流保存为例:打开umeditor.js,找到UM ...

  7. learning svn add file execuable

    svn propset svn:executable on  <file> 为了给svn仓库里的问件添加可执行权限.

  8. Ubuntu 18.04安装fcitx输入法

    1.卸载ibus及所有组件 ----------------------------------------------------------------------------------- ro ...

  9. Ubuntu18.04开机动画(bootsplash)安装

    一.搜索喜欢的主题 1.通过软件源搜索,这个比较简单但是没有太喜欢的.-----------------------------------------------------------pipci@ ...

  10. [转][c++11]我理解的右值引用、移动语义和完美转发

    c++中引入了右值引用和移动语义,可以避免无谓的复制,提高程序性能.有点难理解,于是花时间整理一下自己的理解. 左值.右值 C++中所有的值都必然属于左值.右值二者之一.左值是指表达式结束后依然存在的 ...