Default Converter
from flask import Flask app = Flask(__name__) #/user/123 @('/users/<user_id>') def get_users_data(user_id): return 'get user{}'.format(user_id)
This 123 is the string str
from flask import Flask app = Flask(__name__) #/user/123 @('/users/<int:user_id>') def get_users_data(user_id): return 'get user{}'.format(user_id)
Custom Converter
Defining Methods
Customizing the converter is done in 3 main steps
1. Create a converter class to save the regular expression for matching.
from import BaseConverter class MobileConverter(BaseConverter): """ Cell Phone Number Format """ regex = r'1[3-9]\d{9}]'
Note that the regex name is fixed
2. Inform the Flask application of the customized converter
app = Flask(__name__) # Add the custom converter to the converter dictionary and specify that the converter is used with the name: mobile app.url_map.converters['mobile'] = MobileConverter
3. Define the use of converters where they are used
@('/sms_codes/<mobile:mob_num>') def send_sms_code(mob_num): return 'send sms code to {}'.format(mob_num)
This article on the use of Python_Flask converter is introduced to this article, more related to the use of Python_Flask converter content, please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!