当前位置:网站首页>Flask框架学习:模板渲染与Get,Post请求
Flask框架学习:模板渲染与Get,Post请求
2022-08-11 05:13:00 【weixin_42576837】
模板渲染
所谓模板渲染就是让flask渲染一个html文档,比如你有一个html文件,想要在网站上加载出来,你就要渲染它。
首先把这个文件,叫做模板渲染.html,放在templates文件夹下面,
然后代码中,导入render_template类
from flask import Flask,render_template
另外也可以修改模板文件的渲染路径,使用template_folder来修改
app = Flask(__name__,template_folder='../fdf')
这里我们不修改。
模板渲染.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>模板渲染</title>
</head>
<body>
<p>你好</p>
<h3>How are you</h3>
</body>
</html>

模板渲染.py:
from flask import Flask,render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('模板渲染.html')
if __name__ == '__main__':
app.run()
运行这个flask项目,在根路径下/,执行对应的视图函数,渲染对应的html文件,显示如下:
GET和POST请求
在设置路由的时候,可以设置访问url的时候,接受的请求方式。GET请求表示浏览器需要get某一个文件,服务器就把这个url对应的资源发给浏览器,默认情况下,我们输入url地址,就是在使用GET请求的方式请求资源。比如:
@app.route('/',methods=["GET"])
def index():
return render_template('模板渲染.html')
methods=["GET"]限定访问方式,不写,默认就是GET方式。
表示在访问根路径/时,只接受get方式的请求,那我们输入url,按回车就是get方式,是可以访问的。
如果修改成:@app.route('/',methods=["POST"])
当我们点击这个链接后:
就会发现:
这就是因为我们限制了根路径/的访问只能用POST,当然也可以修改为GET,POST都可以的形式
@app.route('/',methods=["GET","POST"])
POST请求,表示的是我现在不是要获取某个资源,而是我有数据需要提交给服务器,让服务器来处理。
首先,我们的html页面为,这里有一个表单,需要我们填写数据,然后我们把数据交给服务器来处理,设置表单的action="http://localhost:5000/datahandle",也就意味着数据提交到/datahandle这个页面来处理
test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>post请求</title>
</head>
<body>
<form action = "http://localhost:5000/datahandle" method = "post">
<table>
<tr>
<td>Name</td>
<td><input type ="text" name ="username"></td>
</tr>
<tr>
<td>Password</td>
<td><input type ="password" name ="password"></td>
</tr>
<tr>
<td><input type = "submit"></td>
</tr>
</table>
</form>
</form>
</body>
</html>
然后是py文件,
from flask import Flask,render_template,request
app = Flask(__name__)
@app.route('/')
def index():
return render_template('test.html')
#post数据到这个页面来处理,直接输入url是get方式访问,访问不了,这里限定了只能用POST方式访问
@app.route('/datahandle',methods=["POST"])
def handle():
#获取提交到的数据
name = request.form['username']
pwd = request.form['password']
return f'name: {
name}, password: {
pwd}'
if __name__ == '__main__':
app.run()
进入到根路径后会渲染模板文件test.html,然后在表单中输入name,password后,提交给/datahandle页面来处理,它对应的视图函数handle()处理数据。但是这里涉及到如何拿到表单中的数据,需要使用request类
from flask import Flask,render_template,request
name = request.form['username']
pwd = request.form['password']
实际上,在form表单中输入的数据都被flask以字典的形式存储起来了,使用print(request.form)可以查看
表单中输入以下数据:提交,
在控制台中输出ImmutableMultiDict([('username', '123'), ('password', '456')])
键值对username='123',password = '456'
然后使用request.form['username']拿到key对应的value,或者使用request.form.get('username')一样可以拿到数据。
提交后页面跳转到了/datahandle中,处理完的数据返回出来:
成功的拿到了post方式提交的数据。
边栏推荐
- MFC 进程间通信(共享内存)
- redis集群模式--解决redis单点故障
- 【Redis】Redis 的安装及图形化界面 Redis DeskTop Manager 的安装与使用
- Four functional interfaces
- oracle表空间与用户的创建
- MySQL must know and must know (primary articles)
- Weekly recommended short video: your commonly used Polaroid, its predecessor turned out to be like this!
- 一些常见mysql入门练习
- log4j2漏洞复现以及解决方案
- LeetCode刷题Top100之两数之和
猜你喜欢

redis集群模式--解决redis单点故障

实战noVNC全过程操作(包含遇到的问题和解决)

Switch and Router Technology-33-Static NAT

Thymeleaf

玩转mysql之查看mysql版本号

DS220702-0707作业

阿里云无法远程连接数据库MySQL错误码10060解决办法_转载

C statement: data storage
![[Embedded open source library] The use of MultiButton, an easy-to-use event-driven button driver module](/img/7b/e265305df01eb405a131d0de2154d3.png)
[Embedded open source library] The use of MultiButton, an easy-to-use event-driven button driver module

Switch and Router Technology - 32 - Named ACL
随机推荐
redis集群模式--解决redis单点故障
FPGA engineer interview questions collection 111~120
guava RateLimiter均匀限流
MySQL必知必会(初级篇)
Configure checkstyle in IDEA
Switch and Router Technology - 28 - NSSA Areas for OSPF
宝塔Linux环境下redis开启多端口
课堂练习--0708
2022 Quality Officer-Civil Construction Direction-General Basic (Quality Officer) Exam Mock 100 Questions and Online Mock Exam
How IP-Guard prohibits running U disk programs
nodes服务器
Switch and Router Technology-34-Dynamic NAT
Redis details
2022煤矿瓦斯检查考试题模拟考试题库及答案
Oracle常用语句归纳_持续更新
Four functional interfaces
【嵌入式开源库】cJSON的使用,高效精简的json解析库
MFC 进程间通信(共享内存)
Switches and routers technology - 26 - configure OSPF peripheral area
Redis详解