目录
查看电影类型的电影列表
添加电影
修改电影
上机练习 13 使用三层架构完善 web 系统
查看电影类型的电影列表
DAL.py
文件
class MovieDAL(DBHelper):
def getMovieByTid(self,typeid):
sql=f"""select id,title,release_date,score,tname
from MovieType inner join Movie on MovieType.tid =Movie.typeid
where typeid={typeid}"""
return self.cha(sql)
app.py
文件
from DAL import MovieTypeDAL,MovieDAL
mtdal=MovieTypeDAL()
mdal=MovieDAL()
@app.route("/movieList/<typeid>")
def movieList(typeid):
result=mdal.select(typeid)
return render_template("movieList.html",info=result)
添加电影
#app.py
@app.route("/addMovie")
def addMovie():
result=mtdb.select()
return render_template("addMovie.html",info=result)
#addMovie.html
<select name="typeid">
{% for i in info %}
<option value="{{i[0]}}">{{i[1]}}</option>
{% endfor %}
</select>
修改电影
修改电影下拉框参考代码:
app.py
@app.route("/updateMovie/<id>")
def updateMovie(id):
result=mdb.getMovieByID(id)
resultType=mtdb.select()
return
render_template("updateMovie.html",info=result,info1=resultTyp
e,tid=int(result[4]))
修改电影下拉框参考代码:
updateMovie.html
<p>
电影类型:
<select name="typeid">
{% for i in info1 %}
{% if i[0]==tid %}
<option value="{{i[0]}}" selected="selected">{{i[1]}}</option>
{% else %}
<option value="{{i[0]}}">{{i[1]}}</option>
{% endif %}
{% endfor %}
</select>
</p>
上机练习 13 使用三层架构完善 web 系统
查看不同电影类型的电影列表
实现电影的添加
实现电影的修改删除功能,注意刷新页面的处理
电影类型列表页添加模糊搜索电影功能,搜索后跳转到电影列表页
(新增 searchMovie.html)
App.py:(
业务逻辑
)
from flask import Flask,render_template,request
from DAL import MovieTypeDAL,DouBanMovieDAL
from Model import MovieType,DoubanMovieType
app=Flask(__name__)
mtdal=MovieTypeDAL()
mdal=DouBanMovieDAL()
tid_old=0
# 主页列表(全部)
@app.route("/")
def list():result=mdal.select()
# print(result)
return render_template("DouBanMovies.html",info=result)
# 根据电影种类筛选电影
@app.route("/DouBanMoviesSelect", methods=["POST"])
def DouBanMoviesSelect():
keyword=request.form.get("MovieType")
if keyword=="全部":
result=mdal.select()
else:
result=mdal.getMovieByType(keyword)
return render_template("DouBanMovies.html",info=result)
# 根据电影名关键字搜索电影
@app.route("/DouBanMoviesSearch", methods=["POST"])
def DouBanMoviesSearch():
keyword=request.form.get("titlekeywords")
result=mdal.getMovieBytitle(keyword)
return render_template("DouBanMovies.html",info=result)
# 进入添加电影页面
@app.route("/addDouBanMovie")
def add():
return render_template("addDouBanMovie.html")
# 添加提交
@app.route("/DouBanMovieaddSubmit", methods=["POST"])
def addSubmit():
id=request.form.get("id")
title=request.form.get("title")
release_date=request.form.get("release_date")
score=request.form.get("score")
types=request.form.get("types")
mt=DoubanMovieType(id,title,release_date,score,types)
result=mdal.insert(mt)
if result>0:
return "添加成功 <a href='/'>刷新</a>"
else:
return "添加失败 <a href='/'>刷新</a>"
# 删除
@app.route("/deleteDouBanMovie/<id>")
def delete(id):
result=mdal.delete(id)
if result>0:
return "删除成功 <a href='/'>刷新</a>"
else:return "删除失败 <a href='/'>刷新</a>"
# 修改
@app.route("/updateDouBanMovie/<id>")
def update(id):
result=mdal.selectByid(id)
return
render_template("updateDouBanMovies.html",info=result)
# 修改提交
@app.route("/updateDouBanMovieSubmit", methods=["POST"])
def updateSubmit():
# 修改数据
old_id=request.form.get("old_id")
id=request.form.get("id")
title=request.form.get("title")
release_date=request.form.get("release_date")
score=request.form.get("score")
types=request.form.get("types")
result=mdal.update(old_id,"id",id)
result1=mdal.update(old_id,"title",title)
result2=mdal.update(old_id,"release_date",release_date)
result3=mdal.update(old_id,"score",score)
result4=mdal.update(old_id,"types",types)
if result or result4 or result1 or result2 or result3:
return "修改成功 <a href='/'>刷新</a>"
else:
return "修改失败 <a href='/'>刷新</a>"
if __name__=="__main__":
app.run(host="127.0.0.1",port=5000,debug=True)
DAL.py:数据访问
import datetime
import pymysql
class DBHelper:
# 方法
# 数据库链接
def openDB(self):
global conn
conn=pymysql.connect(host="127.0.0.1",user="root",
password="root123456",database="test",charset="utf8")
global baoma
baoma=conn.cursor()# 关闭数据库
def closeDB(self):
baoma.close()
conn.close()
# 数据增删改
def edit(self,sql):
self.openDB()
result=baoma.execute(sql)
conn.commit()
self.closeDB()
return result
# 查询多条
def chaAll(self,sql):
self.openDB()
baoma.execute(sql)
result=baoma.fetchall()
self.closeDB()
return result
# 查询一条
def chaOne(self,sql):
self.openDB()
baoma.execute(sql)
result=baoma.fetchone()
self.closeDB()
return result
class MovieTypeDAL(DBHelper):
# 插入数据
def insert(self,mt):
sql=f"insert into MovieType(tname,tcontent,tdate)
values('{mt.tname}','{mt.tcontent}',now())"
return self.edit(sql)
# 查询数据
def select(self):
sql="select * from MovieType"
return self.chaAll(sql)
# 根据 id 号修改数据
def update(self,tid,UpdateColumn,EndValue):
if type(EndValue)==str:
sql=f"update MovieType set
{UpdateColumn}='{EndValue}' where tid={tid}"
elif type(EndValue)==int :sql=f"update MovieType set {UpdateColumn}={EndValue}
where tid={tid}"
elif type(EndValue)==datetime.datetime:
sql=f"update MovieType set
{UpdateColumn}='{EndValue}' where tid={tid}"
return self.edit(sql)
# 通过 id 号查询数据
def selectByTid(self,tid):
sql=f"select * from MovieType where tid={tid}"
return self.chaOne(sql)
# 通过 id 号删除数据
def delete(self,tid):
sql=f"delete from MovieType where tid={tid}"
return self.edit(sql)
class DouBanMovieDAL(DBHelper):
# 插入数据
def insert(self,dm):
sql=f"insert into
DoubanMovieType(id,title,release_date,score,types)
values('{dm.id}','{dm.title}','{dm.release_date}','{dm.score}',
'{dm.types}')"
return self.edit(sql)
# 查询数据
def select(self):
sql="select * from DoubanMovieType"
return self.chaAll(sql)
# 根据 id 号修改数据
def update(self,id,UpdateColumn,EndValue):
if type(EndValue)==str:
sql=f"update DoubanMovieType set
{UpdateColumn}='{EndValue}' where id={id}"
else :
sql=f"update DoubanMovieType set
{UpdateColumn}={EndValue} where id={id}"
return self.edit(sql)
# 通过 id 号查询数据
def selectByid(self,id):
sql=f"select * from DoubanMovieType where id={id}"
return self.chaOne(sql)
# 通过 id 号删除数据
def delete(self,id):
sql=f"delete from DoubanMovieType where id={id}"return self.edit(sql)
# 根据类型查询电影
def getMovieByType(self,type):
sql=f"""select id,title,release_date,score,types
from DoubanMovieType
where types like '%{type}%'"""
return self.chaAll(sql)
# 根据电影名称查询电影
def getMovieBytitle(self,titlekeywords):
sql=f"""select id,title,release_date,score,types
from DoubanMovieType
where title like '%{titlekeywords}%'"""
return self.chaAll(sql)
model
模型层:
import datetime
class MovieType:
tname=""
tcontent=""
tdate=datetime.datetime.now()
def __init__(self,tname1,tcontent1):
self.tname=tname1
self.tcontent=tcontent1
class DoubanMovieType:
id=""
title=""
release_date=""
score=""
types=""
def __init__(self,id1,title1,release_date1,score1,types1):
self.id=id1
self.title=title1
self.release_date=release_date1
self.score=score1
self.types=types1
addDouBanMovie.html:(
添加电影界面
)
<html>
<head>
<title>添加电影</title>
</head>
<body><h1>欢迎使用豆瓣网</h1>
<a href="/">首页</a>
<a href="">电影类型列表</a>
<a href="">添加电影类型</a>
<a href="/addDouBanMovie">添加电影</a>
<a href="">查看柱状图</a>
<a href="">查看饼状图</a>
<p></p>
<form method="post" action="/DouBanMovieaddSubmit">
<p>电影编号:<input type="text" name="id" /></p>
<p>电影名称:<input type="text" name="title" /></p>
<p>上映时间:<input type="date" name="release_date"
/></p>
<p>评分:<input type="number" name="score" /></p>
<p>电影种类:<input type="text" name="types" /></p>
<!-- 添加按键 -->
<input type="submit" value="添加">
</form>
</body>
<style>
body{ margin: 0 auto; width: 800px;
background-image:url('/static/html 封面图.png');
background-repeat:no-repeat;
background-size: cover;
}
h1{ color: #258dcd}
a{
background-color: #258dcd;
color: white;
padding: 5px 8px;
text-decoration: none;
border-radius: 5px;
/*半透明*/
/*background-color:rgba(55,55,55,0.5)*/
}
</style>
</html>
DouBanMovies.html:(
主页,总表页
)
<html>
<head>
<title>豆瓣网首页</title><meta charset="utf-8"/>
</head>
<body>
<h1>欢迎使用豆瓣网</h1>
<a href="/">首页</a>
<a href="">电影类型列表</a>
<a href="">添加电影类型</a>
<a href="/addDouBanMovie">添加电影</a>
<a href="">查看柱状图</a>
<a href="">查看饼状图</a>
<h1>输入你喜欢的电影进行搜索吧!</h1>
<form method="post" action="/DouBanMoviesSelect">
选择电影种类:
<input type="text" name="MovieType" placeholder="请
输入电影种类">
<input type="submit" value="提交">
</form>
<p></p>
<form method="post" action="/DouBanMoviesSearch">
输入电影名称关键字:
<input type="text" name="titlekeywords" placeholder="
请输入电影名称">
<input type="submit" value="提交">
</form>
<p></p>
<table>
<tr><td>电影编号</td><td>电影名称</td><td>上映时间
</td><td>评分</td><td>电影种类</td></tr>
{% for i in info %}
<tr><td>{{ i[0] }}</td><td>{{ i[1] }}</td><td>{{ i[2]
}}</td><td>{{ i[3] }}</td><td>{{ i[4] }}</td>
<td><a href="/updateDouBanMovie/{{ i[0] }}">修改</a>
<a href="/deleteDouBanMovie/{{ i[0] }}">删除</a></td></tr>
{% endfor %}
</table>
</body>
<style>
body{ margin: 0 auto; width: 800px;
background-image:url('/static/html 封面图.png');
background-repeat:no-repeat;
background-size: cover;
background-position: center;
}h1{ color: #258dcd}
a{
background-color: #258dcd;
color: white;
padding: 1px 12px;
text-decoration: 12px;
border-radius: 12px;
/*半透明*/
/*background-color:rgba(55,55,55,0.5)*/
}
table,tr,td{ border: 2px solid black; border-collapse:
collapse;}
table{ width: 100% ;}
td{ text-align: center; padding: 20px;}
.tou{
background-color: #258dcd;
color: white;
font-size: 18px;
font-weight: bold;
}
</style>
</html>
DouBanMoviesSelect.html:(
模糊筛选页
)
<html>
<head>
<title>筛选结果</title>
<meta charset="utf-8"/>
</head>
<body>
<h1>欢迎使用豆瓣网</h1>
<a href="/">首页</a>
<a href="">电影类型列表</a>
<a href="">添加电影类型</a>
<a href="/addDouBanMovie">添加电影</a>
<a href="">查看柱状图</a>
<a href="">查看饼状图</a>
<form method="post" action="/DouBanMoviesSelect">
<select name="MovieType">
<option type="text" value="全部">全部</option>
<option type="text" value="喜剧">喜剧</option>
<option type="text" value="动作">动作</option>
<option type="text" value="动画">动画</option></select>
<input type="submit" value="提交">
</form>
<form method="post" action="/DouBanMoviesSearch">
<input type="text" name="titlekeywords" placeholder="
请输入电影名称">
<input type="submit" value="提交">
</form>
<p></p>
<table>
<tr><td>电影编号</td><td>电影名称</td><td>上映时间
</td><td>评分</td><td>电影种类</td></tr>
{% for i in info %}
<tr><td>{{ i[0] }}</td><td>{{ i[1] }}</td><td>{{ i[2]
}}</td><td>{{ i[3] }}</td><td>{{ i[4] }}</td>
<td><a href="/updateDouBanMovie/{{ i[0] }}">修改</a>
<a href="/deleteDouBanMovie/{{ i[0] }}">删除</a></td></tr>
{% endfor %}
</table>
</body>
<style>
body{ margin: 0 auto; width: 800px;
background-image:url('/static/html 封面图.png');
background-repeat:no-repeat;
background-size: cover;
background-position: center;
}
h1{ color: #258dcd}
a{
background-color: #258dcd;
color: white;
padding: 1px 12px;
text-decoration: 12px;
border-radius: 12px;
/*半透明*/
/*background-color:rgba(55,55,55,0.5)*/
}
table,tr,td{ border: 2px solid black; border-collapse:
collapse;}
table{ width: 100% ;}
td{ text-align: center; padding: 20px;}
.tou{
background-color: #258dcd;color: white;
font-size: 18px;
font-weight: bold;
}
</style>
</html>
updateDouBanMovies.html:(
修改电影信息页
)
<html>
<head>
<title>修改电影信息</title>
</head>
<body>
<h1>欢迎使用豆瓣网</h1>
<a href="/">首页</a>
<a href="">电影类型列表</a>
<a href="">添加电影类型</a>
<a href="/addDouBanMovie">添加电影</a>
<a href="">查看柱状图</a>
<a href="">查看饼状图</a>
<p></p>
<form method="post" action="/updateDouBanMovieSubmit">
<p>电影编号:<input type="text" name="id"
value="{{ info[0] }}"><input type="hidden" name="old_id"
value="{{ info[0] }}" readonly="readonly"/></p>
<p>电影名称:<input type="text" name="title"
value="{{ info[1] }}"/></p>
<p>上映时间:<input type="date" name="release_date"
value="{{ info[2] }}"/></p>
<p>评分:<input type="number" name="score"
value="{{ info[3] }}"/></p>
<p>电影种类:<input type="text" name="types"
value="{{ info[4] }}"/></p>
<!-- 修改按键 -->
<p><input type="submit" value="修改" /></p>
</form>
</body>
<style>
body{ margin: 0 auto; width: 800px;
background-image:url('/static/html 封面图.png');
background-repeat:no-repeat;
background-size: cover;}
h1{ color: #258dcd}
a{
background-color: #258dcd;
color: white;
padding: 5px 8px;
text-decoration: none;
border-radius: 5px;
/*半透明*/
/*background-color:rgba(55,55,55,0.5)*/
}
</style>
</html>
我的部分代码和老师教的有点出入,所以主界面有点不一样,不过我觉得还行。
各个功能都测试过了。从上午忙活到吃晚饭,头都发昏了!