一、向数据库添加图书数据

【上接】https://blog.csdn.net/u010132177/article/details/103831173

1)首先开启mysql服务,并运行项目

启动mysql服务:
net start mysql80 启动项目:
py manage.py runserver

2)在templates/app1/book.html添加按钮

【1】添加新书按钮 <a href="/detail/{{book.id}}">

hre里的斜杠/默认一定要加上,否则其它页面,如下面的delete中,会导致定向到 /index/delete/不存在 的页面中去

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>书籍页面</title>
</head>
<body>
本站的图书有:<br/>
<ul>
{% for book in books %}
<!--通过href属性,把book的id做为参数传给详情页,进而查询对应英雄信息-->
<li><a href="/detail/{{book.id}}"> {{book.btitle}}</a>:{{book.bpub_date}}</li>
{%empty%}
暂时没有图书!!!
{% endfor %}
</ul>
<!--【1】添加新书按钮-->
<a href="/addInfo">添加一本三国书</a><br/>
</body>
</html>

3)app1/views.py 编写向数据表中增加书籍函数

【0】引入返回请求模块、重新定向模块

【0.1】引用时间模块

【1】添加书籍函数:创建bookinfo对象,并添加一本书

【2.0】成功 return HttpResponse('数据添加成功')

【2】添加成功后重新定向到页面:http://127.0.0.1:8000/books/

from django.shortcuts import render
from app1.models import BookInfo #从模型下导入bookinfo数据模型
from django.http import HttpResponse,HttpResponseRedirect #【0】引入返回请求模块、重新定向模块
from datetime import date #【0.1】引用时间模块 def index(request):
'''app1应用:首页'''
context={} #定义1个字典
context['hello']='hello world!!!' #向字典写一个键:值(hello:'hello world!!')
context['wa']='wawawawawahahahaha!'
context['list']=list(range(1,10)) #定义一个字典值为一个列表,list为把内容转换为列表
return render(request,'app1/index.html',context) #返回:把context渲染到app1/index.html的模板文件 def books(request):
'''app1应用:图书列表页'''
books=BookInfo.objects.all()#从数据库获取图书对象列表
return render(request,'app1/book.html',{'books':books})#把获取到的图书对象赋值给books键。【注意】键'books'必须要加引号 def detail(request,bookId):# bookId为接收urls.py中指定的参数,来源页templates/app1/book.html
'''app1应用:图书详情页,显示英雄信息'''
book=BookInfo.objects.get(pk=bookId) #查询主键为url中传过来的参数Id。或写成:id=bookId
heros=book.heroinfo_set.all() #关联查询:查询对应书的所有英雄信息
return render(request,'app1/detail.html',{'book':book,'heros':heros}) #把参数渲染到detail页面去 def addInfo(request):
'''添加新书到bookinfo表里'''
#【1】创建bookinfo对象,并添加一本书
b=BookInfo()
b.btitle='水浒传'
b.bpub_date=date(1989,9,9)
b.save() #return HttpResponse('数据添加成功')
#【2】添加成功后重新定向到页面:http://127.0.0.1:8000/books/
return HttpResponseRedirect('/books')

4)添加app1/urls.py信息

添加三国书

from django.urls import path,re_path
from . import views urlpatterns=[
path('app1/',views.index),
path('books/',views.books), # 书详情页,通过url接收参数2种写法以下两种都可:
# path(r"detail/<int:bookId>",views.detail), #参数用尖括号包起来<>
re_path(r"^detail/(\d+)",views.detail), #参数必须要带括号 path('addInfo/',views.addInfo),#添加三国书
]

5)效果:http://127.0.0.1:8000/books/

点击后即向数据库添加一本书,添加成功后,重定向回books页面。

二、删除对应图书

1) templates/app1/book.html

关键行:<a href="/delete/{{book.id}}">删除</a>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>书籍页面</title>
</head>
<body>
本站的图书有:<br/>
<ul>
{% for book in books %}
<!--通过href属性,把book的id做为参数传给详情页,进而查询对应英雄信息-->
<li><a href="/detail/{{book.id}}"> {{book.btitle}}</a>:{{book.bpub_date}}-----<a href="/delete/{{book.id}}">删除</a> </li>
{%empty%}
暂时没有图书!!!
{% endfor %}
</ul>
<!--添加新书按钮-->
<a href="/addInfo">添加一本书</a><br/>
</body>
</html>

2 )配置App1/urls.py

关键:path(r'delete/<int:bid>',views.deleteInfo), #删除对应图书

注意:path()间逗号一定别忘记写,否则可能导致未知错误

from django.urls import path,re_path
from . import views urlpatterns=[
path('app1/',views.index),
path('books/',views.books), # 书详情页,通过url接收参数2种写法以下两种都可:
# path(r"detail/<int:bookId>",views.detail), #参数用尖括号包起来<>
re_path(r"^detail/(\d+)",views.detail), #参数必须要带括号 path('addInfo/',views.addInfo), #添加三国书 path(r'delete/<int:bid>',views.deleteInfo), #删除对应图书
]

3)app1/views.py编写删除函数

删除bookinfo id=bid的书籍

from django.shortcuts import render
from app1.models import BookInfo #从模型下导入bookinfo数据模型
from django.http import HttpResponse,HttpResponseRedirect #【0】引入返回请求模块、重新定向模块
from datetime import date #【0.1】引用时间模块 def index(request):
'''app1应用:首页'''
context={} #定义1个字典
context['hello']='hello world!!!' #向字典写一个键:值(hello:'hello world!!')
context['wa']='wawawawawahahahaha!'
context['list']=list(range(1,10)) #定义一个字典值为一个列表,list为把内容转换为列表
return render(request,'app1/index.html',context) #返回:把context渲染到app1/index.html的模板文件 def books(request):
'''app1应用:图书列表页'''
books=BookInfo.objects.all()#从数据库获取图书对象列表
return render(request,'app1/book.html',{'books':books})#把获取到的图书对象赋值给books键。【注意】键'books'必须要加引号 def detail(request,bookId):# bookId为接收urls.py中指定的参数,来源页templates/app1/book.html
'''app1应用:图书详情页,显示英雄信息'''
book=BookInfo.objects.get(pk=bookId) #查询主键为url中传过来的参数Id。或写成:id=bookId
heros=book.heroinfo_set.all() #关联查询:查询对应书的所有英雄信息
return render(request,'app1/detail.html',{'book':book,'heros':heros}) #把参数渲染到detail页面去 def addInfo(request):
'''添加新书到bookinfo表里'''
#【1】创建bookinfo对象,并添加一本书
b=BookInfo()
b.btitle='水浒传'
b.bpub_date=date(1989,9,9)
b.save() #return HttpResponse('数据添加成功')
#【2】添加成功后重新定向到页面:http://127.0.0.1:8000/books/
return HttpResponseRedirect('/books') def deleteInfo(request,bid):
'''删除bookinfo id=bid的书籍'''
#根据传过来的bid查到对应书籍
b=BookInfo.objects.get(id=bid) #也可(主键)pk=bid
b.delete() return HttpResponseRedirect('/books')

效果:http://127.0.0.1:8000/books/

点删除,删除对应图书,并重定向回books页面

三、重定向简写

关键:from django.shortcuts import render,redirect #引入重定向简写模块

使用:return redirect('/books') #【2】简写重定向

app1/views.py

from django.shortcuts import render,redirect #引入重定向简写模块
from app1.models import BookInfo #从模型下导入bookinfo数据模型
from django.http import HttpResponse,HttpResponseRedirect #引入返回请求模块、重新定向模块
from datetime import date # 引用时间模块 def index(request):
'''app1应用:首页'''
context={} #定义1个字典
context['hello']='hello world!!!' #向字典写一个键:值(hello:'hello world!!')
context['wa']='wawawawawahahahaha!'
context['list']=list(range(1,10)) #定义一个字典值为一个列表,list为把内容转换为列表
return render(request,'app1/index.html',context) #返回:把context渲染到app1/index.html的模板文件 def books(request):
'''app1应用:图书列表页'''
books=BookInfo.objects.all()#从数据库获取图书对象列表
return render(request,'app1/book.html',{'books':books})#把获取到的图书对象赋值给books键。【注意】键'books'必须要加引号 def detail(request,bookId):# bookId为接收urls.py中指定的参数,来源页templates/app1/book.html
'''app1应用:图书详情页,显示英雄信息'''
book=BookInfo.objects.get(pk=bookId) #查询主键为url中传过来的参数Id。或写成:id=bookId
heros=book.heroinfo_set.all() #关联查询:查询对应书的所有英雄信息
return render(request,'app1/detail.html',{'book':book,'heros':heros}) #把参数渲染到detail页面去 def addInfo(request):
'''添加新书到bookinfo表里'''
#【1】创建bookinfo对象,并添加一本书
b=BookInfo()
b.btitle='水浒传'
b.bpub_date=date(1989,9,9)
b.save() #return HttpResponse('数据添加成功')
#【2】添加成功后重新定向到页面:http://127.0.0.1:8000/books/
return HttpResponseRedirect('/books') def deleteInfo(request,bid):
'''删除bookinfo id=bid的书籍'''
#根据传过来的bid查到对应书籍
b=BookInfo.objects.get(id=bid) #也可(主键)pk=bid
b.delete() #return HttpResponseRedirect('/books')
return redirect('/books') #【2】简写重定向

效果同上

Django(六)实战2:向数据库添加,删除数据、重定向写法、重定向简写的更多相关文章

  1. 使用Bootstrap + Vue.js实现 添加删除数据

    界面首先需要引入bootstrap的css和bootstrap的js文件,还有vue.js和jQuery.js才可以看见效果. 这里提供bootstrap的在线文件给大家引用: <!-- 最新版 ...

  2. JDBC操作数据库之删除数据

    删除数据使用的SQL语句为delete语句,如果删除图书id为1的图书信息,其SQL语句为: delete from book where id=1 在实际开发中删除数据通常使用PreparedSta ...

  3. Oracle数据库添加删除主外键

    (一)添加主键 1.表创建的同时,添加主键约束 语法: create table "表名" ( "列名1" 数据类型及长度 constraint "主 ...

  4. Python - Django - form 组件动态从数据库取 choices 数据

    app01/models.py: from django.db import models class UserInfo(models.Model): username = models.CharFi ...

  5. Hadoop 添加删除数据节点(datanode)

    前提条件: 添加机器安装jdk等,最好把环境都搞成一样,示例可做相应改动 实现目的: 在hadoop集群中添加一个新增数据节点. 1. 创建目录和用户  mkdir -p /app/hadoop gr ...

  6. 向数据库添加中文数据乱码的解决办法(本文使用spring-jdbcTemplate)

    由于编码字符集的不同通常容易导致数据库中文乱码问题,如显示问号. 往往由以下三个方面所造成的 (一):数据库端字符集设置 1.安装mysql时,会有一个数据库编码设置,将其设置为utf-8 2.先设置 ...

  7. 微信小程序云开发-数据库-用户删除数据

    一.在商品详情页添加[删除单条数据]按钮 进入goodDetail.wxml页面,添加[删除单条数据]按钮,绑定点击事件removeGood()  二.进入goodDetail.js文件,定义remo ...

  8. 用存储过程向数据库添加大量数据【mysql】

    预分配ID的设计,需要先为数据库生成大量的数据.比如对用户ID有要求的系统,那么用户ID就要预先生成. 通过python,php,c/c++/c#,js等程序生成也是可以,但需要这些程序环境,而且单条 ...

  9. 数据库当中删除数据后主键id不连续的问题

    新建查询: ALTER TABLE `表名` DROP `主键名`;ALTER TABLE `表名` ADD `主键名` int NOT NULL FIRST;ALTER TABLE `表名` MOD ...

  10. WPF XML序列化保存数据 支持Datagrid 显示/编辑/添加/删除数据

    XML序列化保存数据 using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...

随机推荐

  1. WPS Linux 2019领先的背后

    导读 过去很多年,作为民族办公领导品牌,金山办公持续高度重视对基础办公软件发展的布局和投入,这其中包括大量的研发资金.人力投入,真正做到了超过30年不忘初心,坚守办公阵地. 早在2019年3月26日, ...

  2. [运维] 如何在 Linux 上安装 Nginx 服务器(一)

    原因 因为小程序对素材的大小是由要求的, 所以为了简化小程序上的内存要求, 在Linux上安装nginx来作为静态资源服务器, 这篇为第一篇, 主要介绍怎么在Linux上安装nginx, 下一篇将会介 ...

  3. redhat 7.6 rsync 配置,实时同步脚本

    1.查看rsync,并安装 yum install rsync -y 2.配置/etc/rsyncd.conf文件 建议cp一份作为备份,清空内容复制以下配置 [服务端配置]log file = /v ...

  4. css怎样让元素显示指定的宽高比

    .father { width: 100% } .child {; padding-bottom: 20%; background: green; overflow: hidden; } <bo ...

  5. Codeforces1301B. Motarack's Birthday

    题意是说给你一串数组,其中-1代表未知,求相邻两个数之差的绝对值最小,-1可以由k赋值,先考虑-1的情况,把k解出来,转换一下,就是绝对值之差最小情况,|k-a|,|k-b|,|k-c|,要使最大的最 ...

  6. 使用JNA替代JNI调用本地方法

    JNA全称是Java Native Access,是Sun推出的一种调用本地方法技术,比起它的同门师兄JNI,JNA大大简化了调用本地方法的过程,使用也比较方便, JNA是在JNI的基础上完善的,用青 ...

  7. windows服务使用和控制启停

    https://www.cnblogs.com/mq0036/p/7875864.html

  8. pycharm不能安装第三方库,错误代码Non-zero exit code (1) 的解决办法

    pycharm版本 2019.3 大致意思是安装失败,建议的解决方案:尝试从系统终端运行此命令.确保使用正确的'pip'版本,该版本已为位于'C:\ Users \ G \ Desktoplgianf ...

  9. STM32CubeMX+FreeRTOS 定时器os_timer的使用

    转载:https://blog.csdn.net/jacklondonjia/article/details/78497120在STM32CubeMX的FreeRTOS配置中,使能FreeRTOS的S ...

  10. MariaDB——备份与恢复

    备份和恢复 为什么要备份?   灾难恢复:硬件故障.软件故障.自然灾害.黑客攻击.误操作   测试   要注意的点:   备份需要多少时间   能够容忍多少的数据丢失   恢复数据需要在多长时间完成  ...