Django(六)实战2:向数据库添加,删除数据、重定向写法、重定向简写
一、向数据库添加图书数据
【上接】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:向数据库添加,删除数据、重定向写法、重定向简写的更多相关文章
- 使用Bootstrap + Vue.js实现 添加删除数据
界面首先需要引入bootstrap的css和bootstrap的js文件,还有vue.js和jQuery.js才可以看见效果. 这里提供bootstrap的在线文件给大家引用: <!-- 最新版 ...
- JDBC操作数据库之删除数据
删除数据使用的SQL语句为delete语句,如果删除图书id为1的图书信息,其SQL语句为: delete from book where id=1 在实际开发中删除数据通常使用PreparedSta ...
- Oracle数据库添加删除主外键
(一)添加主键 1.表创建的同时,添加主键约束 语法: create table "表名" ( "列名1" 数据类型及长度 constraint "主 ...
- Python - Django - form 组件动态从数据库取 choices 数据
app01/models.py: from django.db import models class UserInfo(models.Model): username = models.CharFi ...
- Hadoop 添加删除数据节点(datanode)
前提条件: 添加机器安装jdk等,最好把环境都搞成一样,示例可做相应改动 实现目的: 在hadoop集群中添加一个新增数据节点. 1. 创建目录和用户 mkdir -p /app/hadoop gr ...
- 向数据库添加中文数据乱码的解决办法(本文使用spring-jdbcTemplate)
由于编码字符集的不同通常容易导致数据库中文乱码问题,如显示问号. 往往由以下三个方面所造成的 (一):数据库端字符集设置 1.安装mysql时,会有一个数据库编码设置,将其设置为utf-8 2.先设置 ...
- 微信小程序云开发-数据库-用户删除数据
一.在商品详情页添加[删除单条数据]按钮 进入goodDetail.wxml页面,添加[删除单条数据]按钮,绑定点击事件removeGood() 二.进入goodDetail.js文件,定义remo ...
- 用存储过程向数据库添加大量数据【mysql】
预分配ID的设计,需要先为数据库生成大量的数据.比如对用户ID有要求的系统,那么用户ID就要预先生成. 通过python,php,c/c++/c#,js等程序生成也是可以,但需要这些程序环境,而且单条 ...
- 数据库当中删除数据后主键id不连续的问题
新建查询: ALTER TABLE `表名` DROP `主键名`;ALTER TABLE `表名` ADD `主键名` int NOT NULL FIRST;ALTER TABLE `表名` MOD ...
- WPF XML序列化保存数据 支持Datagrid 显示/编辑/添加/删除数据
XML序列化保存数据 using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...
随机推荐
- Java基础 -5.3
方法的递归调用 指的是一个方法自己调用自己的情况,利用递归调用可以解决一些重复且麻烦的问题 在进行我们递归调用的时候一般要考虑如下几点问题 一定要设置方法递归调用的结束条件 每一次调用的过程之中一定要 ...
- 洗牌函数[打乱数组的顺序] slice()的新运用 [原来arr.slice(start, end) 的start不是必需的]
function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1) + min) } functio ...
- SpringBoot--⼯具表达式对象
⼯具表达式对象除了这些基本的对象之外,Thymeleaf将为我们提供⼀组⼯具对象,这些对象将帮助我们在表达式中执⾏常⻅任务.#execInfo:有关正在处理的模板的信息.#messages:⽤于在变量 ...
- gets和scanf区别
scanf 和 gets 读取字符串 深入了解scanf()/getchar()和gets()等函数 scanf与gets函数读取字符串的区别 今天看到一段话,大致是说gets比scanf()快,有点 ...
- 生成资源文件时候,可以动态替换为maven属性
1.maven管理的文件或者是maven插件处理的文件中 可以引用maven属性,在编译输出时候,可以替换 ${project.build.testOutputDirectory} 在资源 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:"text-danger" 类的文本样式
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- redis队列与RabbitMQ队列区别
消息队列(Message Queue)是一种应用间的通信方式,消息发送后可以立即返回,由消息系统来确保消息的可靠传递.消息发布者只管把消息发布到 MQ 中而不用管谁来取,消息使用者只管从 MQ 中取消 ...
- Windows中使用QEMU创建树莓派虚拟机
环境: windows 10 2018-04-18-raspbian-stretch.img 一.下载QEMU 根据你的系统情况,下载相应的版本,并安装完成 https://www.qemu.org/ ...
- u盘装完centos系统恢复
1.使用windows的cmd窗口,执行diskpart命令 2.执行 list disk命令,查看u盘 3.执行 select disk 2,选中u盘,注意,这里的2是我自己的显示,千万不要选错 4 ...
- 【Android】在程序中使用触力反馈
触力反馈又名:hapticFeedbackEnabled 一般有两种实现方式 第一种是在XML布局文件里面设置 android:hapticFeedbackEnabled="true&quo ...