当前位置:网站首页>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()
边栏推荐
- 面试官:什么是脚手架?为什么需要脚手架?常用的脚手架有哪些?
- Binglog backup data
- "PHP Basics" PHP statements and statement blocks
- regular expression
- openGauss之TryMe初体验
- Graph node deployment and testing
- Massive data Xiao Feng: jointly build and govern opengauss root community and share a thriving new ecosystem
- P7 Day1 get to know the flask framework
- Development of three database general SQL code based on PG Oracle and MySQL
- 【uni-app高级实战】手把手带你学习一个纯实战复杂项目的开发1/100
猜你喜欢
Development of three database general SQL code based on PG Oracle and MySQL

Cache consistency and memory barrier

ERP production operation control Huaxia

Use of "PHP Basics" Boolean

第2章 前台数据展现

I drew a Gu ailing with characters!

百人参与,openGauss开源社区这群人都在讨论什么?

"PHP Basics" tags in PHP

The third letter to the little sister of the test | Oracle stored procedure knowledge sharing and test instructions

Day4 --- flask blueprint and rest ful
随机推荐
MCDF顶层验证方案
Flask one to many database creation, basic addition, deletion, modification and query
虚拟机克隆
Plato farm is expected to further expand its ecosystem through elephant swap
Apache SSI remote command execution vulnerability
Openresty + keepalived to achieve load balancing + IPv6 verification
Interviewer: what is scaffolding? Why do you need scaffolding? What are the commonly used scaffolds?
Help send some recruitment. If you are interested, you can have a look
[MRCTF2020]PYWebsite 1
Breadth first search
SSTI template injection
Fluent rendering mechanism - GPU thread rendering
Virtual machine cloning
JS rotation chart
海关总署:这类产品暂停进口
好吃难吃饱七分为宜;好喝难喝醉三分为佳
Minio 安装与使用
Explain cache consistency and memory barrier
Supervisor 安装与使用
Binglog backup data