Dynamic rules:
Description: URL rules can add variable parts, which means that URLs that conform to the same rules are abstracted into a URL pattern
@app. Route ('/instance/<uuid>/') def instance (UUID): Return ' instance: {} '. Format (UUID)
Note: The content in the angle brackets is dynamic, and any match to the/instance/prefix is mapped to the route, and the UUID is internally given as a parameter, and the default type is string
Default conversions:
Note: The conversion mode can be specified by <converter:variable_name>, Converter can be the default converter as follows It can also be a translator that inherits from the Werkzeug.routing.BaseConverter subclass definition
Conversion (the default converter is stored in the App.url_map.converts dictionary, which supports customization) |
Simple |
String |
Receive text without any slash/ |
Int |
Receive integral type |
Float |
Receive floating-point type |
Path |
Receive text containing/of any text |
Uuid |
Receive unique identification code |
Any () |
Consistent with the built-in any usage in the PY, you can specify a multiple-swollen optional path, but you must pass in the parameter |
Extension: If the upper conversion is based on a subpath, if you do not want to use a sub-path, you can actually pass the argument by get/post the way to/instance, and then inside the view function through Request.args.get () and Request.form.get () To get the implementation
To define the transformation:
Description: Flask also supports custom converters such as Rebbit, which uses delimiters to implement two community names, such as Http://reddit.com/r/python+flask, to facilitate simultaneous viewing of posts from multiple communities, and we can customize and even not necessarily use "+" To achieve
#!/usr/bin/env python# -*- coding: utf-8 -*-"" "## authors: limanman# 51ctobg: http://xmdevops.blog.51cto.com/# purpose:# "" "# Description: Import public module from flask import flask, jsonifyfrom werkzeug.routing import baseconverter# Description: Import other Modules Class listconverter (baseconverter): def __init__ (Self, url_map, separator=u ' + '): super (listconverter, self). __init__ (Url_map) self.separator = separator def to_python (Self, value): return Value.split (Self.separator) def to_url (self, values): values_quoted = [] for Value in values:&nbsP; values_quoted.append (BaseConverter.to_url ( Value)) return self.separator.join (values_quoted) app = flask (__name__) app.url_map.converters.update ({ ' list ': listconverter}) @ App.route ('/r/<list (separator=u "+"):p age_names>/') def r (page_names): return jsonify (page_names) if __name__ == ' __main__ ': app.run (host= ') 0.0.0.0 ', port=9000, debug=true)
Note: Classes that inherit Werkzeug.routing.BaseConverter must implement two methods To_python (value), which defines how the matching path passes through the view function, To_url (value), It defines the encoded form of the path prototype of the parameter of the incoming view function, which is for internal invocation, but also a method that must be implemented, and finally need to put the listconverter into app.url_map.converters to take effect when the HTTP is accessed as above converter ://127.0.0.1:9000/python+flask/The parameter of the incoming view function page_names is actually a segmented list [u ' python ', U ' flask '], so we can simulate Reddit implementation to get multiple community posts simultaneously
Submission Method:
Description: HTTP has multiple methods of accessing URLs, default only for GET, but can change this behavior by passing the methods parameter through the App.route adorner, the default flask automatically handles head/options requests, commonly used get/ Post request but with the popularity of Ajax and reset style applications, Put/delete/patch requests are also widely used ~
#!/usr/bin/env python#-*-coding:utf-8-*-"" "# # authors:limanman# 51ctobg:http://xmdevops.blog.51cto.com/# Purpose:# "" "# Description: Import public module from Flask Import flask, request, jsonify# Description: Import other modules app = Flask (__name__) @app. Route ('/', methods=[' GET ', ' POST ']) def index (): Return jsonify ([Request.args, Request.Form]) if __name__ = = ' __main__ ': app.ru N (host= ' 0.0.0.0 ', port=9000, Debug=true)
Description: Get/post is our most commonly used method, and the parameters to get the Get/post request are Request.args and Request.Form to get the response generated by the jsonify we can see that they are actually two parameter dictionaries
Unique Features:
Note: The rules of flask are based on the Werkzeug routing module, which specifies that the URL is unique, when the adorner @app.route ('/instance/') when the access to/instance is automatically modified to/instance/, And when the adorner @app.route ('/instance ') such as the Access/instance/will return 404, must visit the/instance to get what you want ~ to ensure uniqueness ~
This article is from "@Can up and No BB ..." Blog, be sure to keep this source http://xmdevops.blog.51cto.com/11144840/1866214
The default conversion and custom conversion of site backend _python+flask.0006.flask address translation?