当前位置:网站首页>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方式提交的数据。
边栏推荐
猜你喜欢

0708作业---商品信息

【嵌入式开源库】cJSON的使用,高效精简的json解析库
![[Embedded open source library] The use of cJSON, an efficient and streamlined json parsing library](/img/11/26ec988a23b239d7b01e2e29e3e32d.png)
[Embedded open source library] The use of cJSON, an efficient and streamlined json parsing library

ARM Architecture 4: Embedded Hardware Platform Interface Development

【动态代理】CGLIB 动态代理的使用及原理

Delphi7 learning record - demo example

Weekly recommended short video: your commonly used Polaroid, its predecessor turned out to be like this!

Switch and Router Technology-34-Dynamic NAT

【ARM】rk3399挂载nfs报错

Trilium使用总结
随机推荐
提升你工作效率的技巧,你得知道——Navitcat 快捷键
Win10远程连接(实现多用户同时连接)
Idea essential skills to improve work efficiency
Idea提升工作效率的必备技巧
4 Module 3: Literature Reading and Research Methods
JedisLock_Redis分布式锁实现_转载
MFC 进程间通信(共享内存)
Golden Warehouse Database KingbaseGIS User Manual (6.8. Geometry Object Input Function)
Mysql入门练习
MyEclipse数据库工具使用教程:使用驱动程序
2022 Quality Officer-Civil Construction Direction-General Basic (Quality Officer) Exam Mock 100 Questions and Online Mock Exam
Paper Notes: Bag of Tricks for Long-Tailed Visual Recognition with Deep Convolutional Neural Networks
Prometheus :(一)基本概念
Redis-数据类型(基本指令、String、List、Set、Hash、ZSet、BitMaps、HyperLogLog、GeoSpatial)/发布和订阅
并发编程之线程基础
Unity WebGL RuntimeError: integer overflow
K8s复习笔记7--Redis单机和Redis-cluster的K8S实现
Idea 2021.3.3版本文件目录展开
oracle tablespace and user creation
[Embedded open source library] The use of cJSON, an efficient and streamlined json parsing library