Flask URL Converter, add alternative URLs

Source: Internet
Author: User

Import urllibfrom flask Import flaskfrom werkzeug.routing Import Baseconverterapp = Flask (__name__) class Listconverter (        Baseconverter): Def __init__ (self, url_map, separator= "+"): Super (Listconverter, self). __init__ (Url_map)  Self.separator = Urllib.unquote (separator) def to_python (self, value): Return Value.split (Self.separator) def To_url (self, Values): Return Self.separator.join (Baseconverter.to_url (value) for value in values) ' app.url_map.converters{' default ': <class ' werkzeug.routing.UnicodeConverter ';, ' string ': <class ' werkzeug.routing.UnicodeConverter ', ' int ': <class ' werkzeug.routing.IntegerConverter ', ' path ' : <class ' werkzeug.routing.PathConverter ';, ' float ': <class ' werkzeug.routing.FloatConverter ';, ' any ': <class ' werkzeug.routing.AnyConverter ', ' uuid ': <class ' Werkzeug.routing.UUIDConverter ';} ' App.url_ map.converters[' list ' = Listconverter@app.route ('/list1/<list:page_names>/') def list1 (page_names): Return ' Separator: {} {} '. Format ("+", Page_names) @app. Route ('/list2/ <list (separator=u "|"):p age_names>/') def list2 (page_names): Return ' separator: {} {} '. Format ("|", Page_names) if __name__ = = "__main__": App.run ()

http://127.0.0.1:5000/list1/a+b/

Separator: + [u ' a ', U ' B ']

http://127.0.0.1:5000/list2/a|b/

Separator: | [u ' a ', U ' B ']

Custom converters need to integrate baseconverter to implement To_python and To_url two methods

To_python: Converting a path to a Python object

To_url: Converting a path to a URL-compliant format

From the "Python Web Development Combat"

Flask URL Converter, add alternative URLs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.