Flask 11th--Custom URL converters

Source: Internet
Author: User
Tags uuid

First and everyone to say the composition of flask, flask is actually werkzeug(路由和http处理) + SQLAlchemy(数据库) + Jinja2(模板) composition, today we want to see something is ' Werkzeug ' inside content.

We import in the code from werkzeug.routing import BaseConverter , and then go to BaseConverter look at the source of this method:

to_pythonthe function of the method is to convert the parameters you passed to the corresponding type of data, such as you set the parameter is the UUID type data, then when you pass the argument, you will call the To_python method, the parameter is converted to the corresponding UUID type.

In addition, below, PathConverter and UUIDConverter so on, and so on, we will see, originally here to write a map, whenever we pass in the URL of the specified type of parameters, the system will automatically look for the mapping here:

So now we want to customize the URL converter--we want to pass in a list type of data, then we can refer to the form in the above code, define to_python and To_url methods, and add to the Default_converters map. Now we assume that the obtained parameter is [1, 2, 3], we want to take it 1+2+3的形式在url中 , then the to_python task of our method is to remove the + in the URL 1+2+3 ; we know that Python's split method can remove the +, but it returns a list of lists, And the address in the URL is not likely to appear in the list, that is, you can not see the address is 127.0.0.1:500/[1,2,3]/ this form, so our other task is to convert the array into the form we need ' 1+2+3 '. Note that our URL address parameter is in the code is flask.url_for (' detail ', params=[1, 2, 3]), that is, the address is transmitted in the code,

We've already figured out the address of the page:

# Coding:utf-8

From flaskImport Flask
From werkzeug.routingImport Baseconverter
Import Flaskapp = Flask (__name__)# Type:flask
App.debug =True

# root directory
@app. Route ('/')
DefHello_world():Return' Hello world! '

ClassListconverter(Baseconverter):DefTo_python(self, values):"" To convert the parameters in the URL to the data type we need "" "# The Split method is to remove the plus sign and return the list type data TMP = Values.split (' + ')
Print' tmp:%s '% tmp
return tmp

DefTo_url(self, values):"" "Convert [1+2+3] to" ""# Iterate through the data in the list values, connect with +, and the last Tmp1 value is 1+2+3
# Baseconverter.to_url is encoding the URL tmp1 =' + '. Join ([Baseconverter.to_url (self, value) to value in values])
print ' tmp1:%s '% TMP1
return TMP1


# Register the well-written class to Default_converters
app.url_map.converters[' list ' = Listconverter

@app. Route ('/detail/<list:params>/')
def detail(params): print ' parmas:%s '% params
return ' success for URL '

With App.test_request_context ():
The URL of the print ' detail function is:%s '% flask.url_for (' detail ', params=[1, 2, 3])


if __name__ = = ' __main__ ': App.run () /c12>

After executing the code, refresh the page to see:

    • Control Desk

tmp1:1+2+3detail函数的url是:/detail/1+2+3/ * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Restarting with stattmp1:1+2+3detail函数的url是:/detail/1+2+3/ * Debugger is active! * Debugger PIN: 176-123-878127.0.0.1 - - [15/Mar/2018 00:21:23] "GET /detail/1+2+3/ HTTP/1.1" 200 -tmp: [u‘1‘, u‘2‘, u‘3‘]params:[u‘1‘, u‘2‘, u‘3‘]tmp: [u‘1‘, u‘2‘, u‘3‘]127.0.0.1 - - [15/Mar/2018 00:23:28] "GET /detail/1+2+3/ HTTP/1.1" 200 -params:[u‘1‘, u‘2‘, u‘3‘]
    • Page

      地址:http://127.0.0.1:5000/detail/1+2+3/页面显示:success for url

Here is a bit difficult to understand, it is hard to understand, we understand better, do not understand to know there is so good, there will be a note here.

Please pay attention to the public number: Automated test practice, view clear layout and pictures

Flask 11th--Custom URL converters

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.