Python - Django - 添加作者
在 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 - 添加作者的更多相关文章
- Python - Django - 编辑作者
在作者列表页面的操作栏中加上编辑按钮 <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- Python - Django - 删除作者
修改 author_list.html,添加删除按钮 <!DOCTYPE html> <html lang="en"> <head> <m ...
- Python - Django - 显示作者列表
在 views.py 中添加展示作者列表的函数 from django.shortcuts import render, redirect, HttpResponse from app01 impor ...
- Python - Django - 添加首页尾页上一页下一页
添加首页和尾页: views.py: from django.shortcuts import render from app01 import models def book_list(reques ...
- python Django教程 之 模型(数据库)、自定义Field、数据表更改、QuerySet API
python Django教程 之 模型(数据库).自定义Field.数据表更改.QuerySet API 一.Django 模型(数据库) Django 模型是与数据库相关的,与数据库相关的代码 ...
- python——django使用mysql数据库(一)
之前已经写过如何创建一个django项目,现在我们已经有了一个小骷髅,要想这个web工程变成一个有血有肉的人,我们还需要做很多操作.现在就先来介绍如何在django中使用mysql数据库. 前提:已经 ...
- python Django教程 之模板渲染、循环、条件判断、常用的标签、过滤器
python3.5 manage.py runserver python Django教程 之模板渲染.循环.条件判断.常用的标签.过滤器 一.Django模板渲染模板 1. 创建一个 zqxt_tm ...
- python Django教程 之 安装、基本命令、视图与网站
python Django教程 之 安装.基本命令.视图与网站 一.简介 Django 中提供了开发网站经常用到的模块,常见的代码都为你写好了,通过减少重复的代码,Django 使你能够专注于 w ...
- python django 多级业务树形结构规划及页面渲染
概述: 在项目中,父级到子级结构并不少见,如果仅仅的两层树形结构,我们可以使用数据库的外键设计轻松做到,子级业务表设计一字段外键到父级业务表,这样子到父.父到子的查询都非常简单. 但是往往父子结构会有 ...
随机推荐
- nginx 环境 thinkphp 隐藏index.php
tp官网已经写了 http://doc.thinkphp.cn/manual/hidden_index.html 不生效 重启nginx .问题依旧 kill掉nginx进程 再启动 贴段自己的配置 ...
- matlab(8) Regularized logistic regression : 不同的λ(0,1,10,100)值对regularization的影响,对应不同的decision boundary\ 预测新的值和计算模型的精度predict.m
不同的λ(0,1,10,100)值对regularization的影响\ 预测新的值和计算模型的精度 %% ============= Part 2: Regularization and Accur ...
- urlrewrite与struts2结合使用基本配置
1.更改web.xml,,,在struts2拦截器前面添加urlrewrite配置信息,,默认是forward的 <filter> <filter-name>UrlRewrit ...
- 使用JSP/Servalet技术开发新闻发布系统------JSP数据交互一
什么是内置对象 JSP内置对象是 Web 容器创建的一组对象,不用通过手动new就可以使用 JSP中的九大内存对象 request 请求对象 response 响应对象 out 输出对象 ...
- MySQL高级 之 explain执行计划详解(转)
使用explain关键字可以模拟优化器执行SQL查询语句,从而知道MySQL是如何处理你的SQL语句的,分析你的查询语句或是表结构的性能瓶颈. explain执行计划包含的信息 其中最重要的字段为:i ...
- 动态menu导航条以及treeview树
1.menu表数据 2.在后台生成html内容后,前台利用nav-h.css生成menu导航条,利用Jquery的treeview插件生成menu树 前台coding: <!DOCTYPE ht ...
- 关于 js 函数参数的this
先看一道面试题: var number = 10; function fn() { console.log(this.number); } var obj = { number: 2, show: f ...
- Vue.config.productionTip = false 是什麽意思
阻止启动生产消息,常用作指令. 阻止启动生产消息 這又是什麽意思? 看下效果 Vue.config.productionTip = false Vue.config.productionTip = t ...
- CSP初赛复习
初赛复习 初赛一定要过啊,否则付出的那么多都白搭了! while(1) ++csp.rp,++csp.luck,++csp.scores; 历史 2020年开始,除NOIP以外的NOI系列其他赛事(包 ...
- fopen 的mode
转自 http://blog.csdn.net/todd911/article/details/8976543 r 打开只读文件,该文件必须存在. r+具有读写属性,从文件头开始写,保留原文件中没有被 ...