当前位置:网站首页>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的时候,接受的请求方式.GET
The request indicates what the browser needsget某一个文件,The server takes thisurlThe corresponding resource is sent to the browser,默认情况下,我们输入url地址,就是在使用GET
Requests 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/datahandle
this 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方式提交的数据.
边栏推荐
- 实战noVNC全过程操作(包含遇到的问题和解决)
- 面试宝典二:nlp常见知识点
- Use Adobe genuine software for prostitution to reduce the slow employment and non-employment of fresh graduates
- 2022 Quality Officer-Civil Construction Direction-General Basic (Quality Officer) Exam Mock 100 Questions and Online Mock Exam
- ARM Architecture 4: Embedded Hardware Platform Interface Development
- Day38 LeetCode
- prometheus:(二)监控概述(你永远逃不出我的手掌哈哈)
- 我的四核Cortex-A53学习之路
- [No 2022 Shanghai Security Officer A Certificate Exam Question Bank and Mock Exam
- pytorch和tensorflow函数对应表
猜你喜欢
Keras与tensorflow 使用基础
【嵌入式开源库】MultiTimer 的使用,一款可无限扩展的软件定时器
总结:交叉验证
Oracle中如何用一个表的数据更新另一个表中的数据_转载
什么是三次握手和四次挥手(清晰易懂)
prometheus:(二)监控概述(你永远逃不出我的手掌哈哈)
Idea essential skills to improve work efficiency
Switch and Router Technology-35-NAT to PAT
The most complete installation tutorial of Pytorch (one step)
Thymeleaf
随机推荐
分库分表之sharding-proxy
Mysql introductory exercise
【嵌入式开源库】MultiButton的使用,简单易用的事件驱动型按键驱动模块
Golden Warehouse Database KingbaseGIS User Manual (6.8. Geometry Object Input Function)
【Redis】Redis 的安装及图形化界面 Redis DeskTop Manager 的安装与使用
Keras与tensorflow 使用基础
Redis - Data Types (Basic Instructions, String, List, Set, Hash, ZSet, BitMaps, HyperLogLog, GeoSpatial) / Publish and Subscribe
Internet Protocol 1
guava RateLimiter uniform current limit
【嵌入式开源库】cJSON的使用,高效精简的json解析库
ARM Architecture 4: Embedded Hardware Platform Interface Development
Prometheus :(一)基本概念
Oracle常用语句归纳_持续更新
IDEA使用记录
[Embedded open source library] The use of cJSON, an efficient and streamlined json parsing library
【嵌入式开源库】使用J-Link打印日志,让你节省一个打印串口
Four functional interfaces
Redis details
JedisLock_Redis分布式锁实现_转载
tensorflow代码翻译成pytorch代码 -详细教程+案例