当前位置:网站首页>Flask (IV) -- URL construction
Flask (IV) -- URL construction
2022-06-11 09:27:00 【zju_ cbw】
url_for()Functions are important for building specific functions dynamically URL Very useful . This function takes the name of the function as the first argument , And accept one or more keyword parameters , Each parameter corresponds to URL The variable part of .
The following script demonstrates using url_for() function .
from flask import Flask, redirect, url_for
app = Flask(__name__)
@app.route('/admin')
def hello_admin():
return 'Hello Admin'
@app.route('/guest/<guest>')
def hello_guest(guest):
return 'Hello %s as Guest' % guest
@app.route('/user/<name>')
def user(name):
if name =='admin':
return redirect(url_for('hello_admin'))
else:
return redirect(url_for('hello_guest',guest = name))
if __name__ == '__main__':
app.run(debug = True)
The above script has a user function ( name ), It receives from URL Parameter values for .
User() Function to check whether the received parameter is consistent with admin matching . If the match , Then use url_for() Redirect application to hello_admin() function , Otherwise, take the received parameter as guest Parameter passed to hello_guest() function .
Run the script above , Open the browser and type URL - http://localhost:5000/user/admin. The browser outputs the following Hello Admin
Enter the following in the browser URL - http://localhost:5000/user/mvl. The browser outputs the following Hello mvl as Guest
边栏推荐
- 考研數學 【數列極限證明題】題型方法總結
- 机器学习笔记 - 使用TensorFlow的Spatial Transformer网络
- ERP体系的这些优势,你知道吗?
- Flutter development log - route management
- CUMT learning diary - theoretical analysis of uCOSII - Textbook of Renzhe Edition
- 企业需要考虑的远程办公相关问题
- 1400. 构造 K 个回文字符串
- 报错Version mismatch between installed depthai lib and the required one by the scrip.
- Set up redis highly available cluster environment
- DOS command virtual environment
猜你喜欢

机器学习笔记 - 使用TensorFlow的Spatial Transformer网络

Method (common method), method execution memory analysis, method overloading mechanism, method recursion

Day44 database

ArcGIS 10.9.1 地质、气象体元数据处理及服务发布调用

Résumé de la méthode d'examen des mathématiques

Type-C蓝牙音箱单口可充可OTG方案

Set up redis highly available cluster environment

Identifier keyword literal data type base conversion character encoding variable data type explanation operator

Talk about reading the source code

Sed explanation of shell script (SED command, sed -e, sed s/ new / old /...)
随机推荐
Openstack explanation (22) -- neutron plug-in configuration
Do you know these advantages of ERP system?
Fabric. JS dynamically set font size
Flutter development log - route management
基于SIC32F911RET6设计的腕式血压计方案
面试题 17.10. 主要元素
实现边充边OTG的PD芯片GA670-10
Output image is bigger (1228800b) than maximum frame size specified in properties (1048576b)
[image processing] spatial domain image enhancement
【分享】企業如何進行施行規劃?
MSF基于SMB的信息收集
Pulsar job Plaza | Tencent, Huawei cloud, shrimp skin, Zhong'an insurance, streamnational and other hot jobs
Complexity analysis of matrix inversion operation (complexity analysis of inverse matrix)
Runtimeerror: blobreader error:the version of imported blob doesn't match graph_ transformer
[software] ERP model selection method for large enterprises
openstack详解(二十二)——Neutron插件配置
682. baseball game
Console you don't know
[TiO websocket] v. TiO websocket server counts the number of online people
MSF evasion模块的使用