当前位置:网站首页>Flask framework learning: template rendering and Get, Post requests
Flask framework learning: template rendering and Get, Post requests
2022-08-11 05:33:00 【weixin_42576837】
模板渲染
The so-called template rendering is to letflask渲染一个html文档,比如你有一个html文件,Want to load it up on the website,You're going to render it.
First put this file,叫做模板渲染.html,放在templates文件夹下面,
然后代码中,导入render_template类
from flask import Flask,render_template
You can also modify the rendering path of the template file,使用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请求
when setting up routing,可以设置访问url的时候,接受的请求方式.GETThe request indicates what the browser needsget某一个文件,The server takes thisurlThe corresponding resource is sent to the browser,默认情况下,我们输入url地址,就是在使用GETRequests for resources.比如:
@app.route('/',methods=["GET"])
def index():
return render_template('模板渲染.html')
methods=["GET"]限定访问方式,不写,默认就是GET方式.
Indicates that the root path is being accessed/时,只接受get方式的请求,那我们输入url,press enter isget方式,是可以访问的.
如果修改成:@app.route('/',methods=["POST"])
After we click on this link:
就会发现:
This is because we restrict the root path/access is only availablePOST,Of course it can also be modified as GET,POSTany form
@app.route('/',methods=["GET","POST"])
POST请求,It means that I am not trying to get a resource now,Instead I have data that needs to be submitted to the server,Let the server handle it.
首先,我们的html页面为,Here is a form,We need to fill in the data,Then we hand the data over to the server for processing,设置表单的action="http://localhost:5000/datahandle",That means the data is submitted to/datahandlethis page to handle
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')
#postdata to this page for processing,直接输入url是get方式访问,访问不了,This is limited to use onlyPOST方式访问
@app.route('/datahandle',methods=["POST"])
def handle():
#Get the data submitted to
name = request.form['username']
pwd = request.form['password']
return f'name: {
name}, password: {
pwd}'
if __name__ == '__main__':
app.run()
After entering the root path, the template file will be renderedtest.html,Then enter in the formname,password后,提交给/datahandle页面来处理,Its corresponding view functionhandle()处理数据.But here is how to get the data in the form,需要使用request类
from flask import Flask,render_template,request
name = request.form['username']
pwd = request.form['password']
实际上,在formThe data entered in the form are all takenflaskstored in the form of a dictionary,使用print(request.form)可以查看
Enter the following data into the form:提交,
在控制台中输出ImmutableMultiDict([('username', '123'), ('password', '456')])
键值对username='123',password = '456'
然后使用request.form['username']拿到key对应的value,或者使用request.form.get('username')The same data can be obtained.
After submitting, the page jumps to/datahandle中,The processed data is returned:
成功的拿到了post方式提交的数据.
边栏推荐
猜你喜欢

玩转mysql之查看mysql版本号

prometheus:(二)监控概述(你永远逃不出我的手掌哈哈)

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

UML基本概念——动态视图

(一)性能实时监控平台搭建(Grafana+Influxdb+Jmeter)

每周推荐短视频:你常用的拍立淘,它的前身原来是这样的!

四大函数式接口

【Redis】Redis 的安装及图形化界面 Redis DeskTop Manager 的安装与使用

Configure checkstyle in IDEA

CentOS7静默安装Oracle11g_转载
随机推荐
curl 命令调用接口demo
The most complete installation tutorial of Pytorch (one step)
2022 building welder (building a special type of work) examination questions and simulation test
批量修改数据库等视频文件名称
原生态mongo连接查询代码
Unity WebGL RuntimeError: integer overflow
总结:交叉验证
Trilium使用总结
IDEA中配置checkstyle
pip 国内源下载
LeetCode刷题Top100之两数之和
2022建筑焊工(建筑特殊工种)考题及模拟考试
基于 TF-IDF 文本匹配实战详细教程 数据+代码 可直接运行
prometheus:(二)监控概述(你永远逃不出我的手掌哈哈)
Decryption of BitLocker
CentOS7静默安装Oracle11g_转载
JedisLock_Redis分布式锁实现_转载
[No 2022 Shanghai Security Officer A Certificate Exam Question Bank and Mock Exam
Internet Protocol 1
第二篇 DS5 Armv8 样例工程报错之GCC编译