当前位置:网站首页>Flask的传参以及返回的响应
Flask的传参以及返回的响应
2022-07-27 05:02:00 【pink_Pig___】
传参
- 固定参数
from flask import Flask
app.Flask(__name__)
# <int:整型>/<string:字符串>/<默认字符串>
@app.route('/hello/<int:id>')
def hello(id)
return f'接收到的ID为:{
id}'
if __name__ == '__main__':
app.run()
- 自定义转换器
from flask import Flask
from flask import BaseConverter
app = Flask(__name)
# 自定义转换器
class PhoneConverter(BaseConverter):
# regex固定变量
regex = r'1[3-9][0-9]{9}'
app.url_map.converters['phone'] = PhoneConverter
@app.route('/phone/<phone:my_phone>'
def get_phone(my_phone):
return f'手机号为{
my_phone}'
if __name__ == '__main__':
app.run()
- 查看传递参数’get’
from flask import Flask
from flask import request
# 查看传递参数
@app.route('/query')
def query():
params = request.args.to_dict() # 把不可变字典转换为普通字典
print(params)
name = request.args['name']
age = request.args.get('age')
return f'当前名字为:{
name}, 年龄为:{
age}'
if __name__ == '__main__':
app.run()
- 表单的获取 ‘post’
from flask import Flask
from flask import request
@app.route('/form', methods=['post'])
def form():
name = request.form.get('name')
age = request.form.get('age')
return f'name:{
name}, age:{
age}'
if __name__ == '__main__':
app.run()
- 上传文件
from flask import Flask
from flask import request
@app.route('/upload', methods=['post'])
def upload():
img = request.files.get('img')
print(img)
# 保存到当前目录下的static里
img.save('./static/01.png')
return '上传成功'
if __name__ == '__main__':
app.run()
- 其他参数
from flask import Flask
from flask import request
@app.route('/other')
def other():
print(request.headers)
print(request.method)
print(request.url)
return '其他参数'
if __name__ == '__main__':
app.run()
响应的返回
- 返回HTML页面的数据
from flask import Flask
from flask import render_template
app.Flask(__name__)
@app.route('/page')
def page():
return render_template('hello.html')
if __name__ == '__main__':
app.run()
- 返回响应时跳转页面
from flask import Flask
from flask import redirect
app.Flask(__name__)
@app.route('/jump')
def jump():
return redirect('https://www.baidu.com')
if __name__ == '__main__':
app.run()
- 返回字典
from flask import Flask
from flask import jsonify
app.Flask(__name__)
@app.route('/return_json')
def return_json():
data = {
'name': 'zhangsan',
'age': 18,
'action': ['吃', '睡觉', '上厕所']
}
return jsonify(data)
if __name__ == '__main__':
app.run()
- 返回元组
from flask import Flask
app.Flask(__name__)
@app.route('/return_json')
def return_json():
return ('hello', 5400,{
'aaa': 'bbbb'})
if __name__ == '__main__':
app.run()
- 自定义响应对象
from flask import Flask
from flask import make_response
app.Flask(__name__)
@app.route('/return_json')
def return_obj():
resp = make_response('hello')
resp.status = '404'
resp.headers['aaa'] = 'bbb'
return resp
if __name__ == '__main__':
app.run()
边栏推荐
- Message reliability processing
- JVM上篇:内存与垃圾回收篇八--运行时数据区-方法区
- 稀疏数组→五子棋的存盘续盘等操作
- JVM Part 1: memory and garbage collection part 12 -- stringtable
- Summary of knowledge points (I)
- Two dimensional array summation exercise
- ERP system brand
- Static and final keyword learning demo exercise
- 辗转相除法
- The provision of operation and maintenance manager is significantly affected, and, for example, it is like an eep command
猜你喜欢

JVM Part 1: memory and garbage collection part 7 -- runtime data area heap

Machine learning overview

torch中乘法整理,*&torch.mul()&torch.mv()&torch.mm()&torch.dot()&@&torch.mutmal()

mq设置过期时间、优先级、死信队列、延迟队列

李宏毅机器学习组队学习打卡活动day03---误差和梯度下降

Explore the mysteries of the security, intelligence and performance of the universal altek platform!

Prime number screening (Ehrlich sieve method, interval sieve method, Euler sieve method)

Scientific Computing Library - numpy

How to test the payment process?

Utility gadget: kotlin code snippet
随机推荐
Differences among left join, inner join and right join
Solution and principle analysis of feign call missing request header
Explore the mysteries of the security, intelligence and performance of the universal altek platform!
求组合数(最强优化)
Find the number of combinations (the strongest optimization)
B1021 个位数统计
2021 OWASP top 5: security configuration error
Critical path principle
秒杀系统设计
Card drawing program simulation
redis持久化
JVM Part 1: memory and garbage collection part 6 -- runtime data area local method & local method stack
322 coin change of leetcode
LeetCode之6 ZigZag Conversion
[acwing] solution to the 61st weekly match
Use ngrok for intranet penetration
The receiver sets the concurrency and current limit
During its low-level period, this slave edge causes the instruction number to make a corresponding model
Bean的生命周期&&依赖注入*依赖自动装配
Prime number screening (Ehrlich sieve method, interval sieve method, Euler sieve method)