当前位置:网站首页>Pass parameters and returned responses of flask
Pass parameters and returned responses of flask
2022-07-27 08:33:00 【pink_ Pig___】
The ginseng
- Fixed parameter
from flask import Flask
app.Flask(__name__)
# <int: integer >/<string: character string >/< Default string >
@app.route('/hello/<int:id>')
def hello(id)
return f' The received ID by :{
id}'
if __name__ == '__main__':
app.run()
- Custom converter
from flask import Flask
from flask import BaseConverter
app = Flask(__name)
# Custom converter
class PhoneConverter(BaseConverter):
# regex Fixed variable
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' The cell phone number is {
my_phone}'
if __name__ == '__main__':
app.run()
- View the transfer parameters ’get’
from flask import Flask
from flask import request
# View the transfer parameters
@app.route('/query')
def query():
params = request.args.to_dict() # Convert an immutable dictionary into an ordinary dictionary
print(params)
name = request.args['name']
age = request.args.get('age')
return f' The current name is :{
name}, Age is :{
age}'
if __name__ == '__main__':
app.run()
- Acquisition of forms ‘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()
- Upload files
from flask import Flask
from flask import request
@app.route('/upload', methods=['post'])
def upload():
img = request.files.get('img')
print(img)
# Save to... In the current directory static in
img.save('./static/01.png')
return ' Upload successful '
if __name__ == '__main__':
app.run()
- The other parameters
from flask import Flask
from flask import request
@app.route('/other')
def other():
print(request.headers)
print(request.method)
print(request.url)
return ' The other parameters '
if __name__ == '__main__':
app.run()
Return of response
- return HTML Page data
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()
- Jump to the page when returning the response
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()
- Return dictionary
from flask import Flask
from flask import jsonify
app.Flask(__name__)
@app.route('/return_json')
def return_json():
data = {
'name': 'zhangsan',
'age': 18,
'action': [' eat ', ' sleep ', ' Use the toilet ']
}
return jsonify(data)
if __name__ == '__main__':
app.run()
- Return a tuple
from flask import Flask
app.Flask(__name__)
@app.route('/return_json')
def return_json():
return ('hello', 5400,{
'aaa': 'bbbb'})
if __name__ == '__main__':
app.run()
- Custom response object
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()
边栏推荐
- 第2章 前台数据展现
- 好吃难吃饱七分为宜;好喝难喝醉三分为佳
- openGauss之TryMe初体验
- [MRCTF2020]PYWebsite 1
- docker 安装mysql后进入容器内部发现登录不了mysql
- Functions and arrow functions
- [netding cup 2020 Qinglong group]areuserialz (buuctf)
- Realization of background channel group management function
- How to merge multiple columns in an excel table into one column
- List删除集合元素
猜你喜欢

UVM Introduction Experiment 1

OPPO 自研大规模知识图谱及其在数智工程中的应用

"PHP Basics" tags in PHP

Minio 安装与使用

Element display mode: block level, inline, inline block, nesting specification, display mode conversion

Process control - Branch

MCDF top level verification scheme
Development of three database general SQL code based on PG Oracle and MySQL

Minio installation and use

Risk control and application of informatization project
随机推荐
Interviewer: what is scaffolding? Why do you need scaffolding? What are the commonly used scaffolds?
Massive data Xiao Feng: jointly build and govern opengauss root community and share a thriving new ecosystem
Shenzhi Kalan Temple
Day4 --- flask blueprint and rest ful
Set set
How to uninstall -- Qianxin secure terminal management system
SSTI template injection
arguments
vCenter7.0管理Esxi7.0主机
[MRCTF2020]PYWebsite 1
1176 questions of Olympiad in informatics -- who ranked K in the exam
MCDF顶层验证方案
Using ecological power, opengauss breaks through the performance bottleneck
JS advanced knowledge - function
Attack and defense World Lottery
JS rotation chart
Graph node deployment and testing
Explain cache consistency and memory barrier
Netdata 性能监测工具介绍、安装、使用
Make a game by yourself with pyGame 01