Today I'm going to talk about the mapping between URL and view function
The mapping between url and view function is through@ app.route() implemented by the decorator.
1. Only one slash represents the root directory - the home page.# coding: utf-8 from flask import Flask # Wei name__ Is the main file used to determine the operation of the flash app = Flask(__name__) # type: Flask app.debug = True # app.config.from_object('configs') @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()
Code above@ app.route('/') the decorator parameter has only one slash /, at this time, the running code will jump to the root directory - home page. As shown in the figure:
2. Biography of ginsengURL parameters are passed in the form of < parameter name >. In addition, there are several parameters in the URL, and several parameters should be specified in the view function. The parameter name can be defined by itself. The code is as follows:
# coding: utf-8 from flask import Flask # Wei name__ Is the main file used to determine the operation of the flash app = Flask(__name__) # type: Flask app.debug = True # app.config.from_object('configs') # root directory @app.route('/') def hello_world(): return 'Hello World!' # Biography of ginseng @app.route('/content/<username>/<password>/') def login(username, password): return u'My user name is:%s,The password is:%s' % (username, password) if __name__ == '__main__': app.run()
After executing the code, enter the address you set in the browser and you can see:
It should be noted that the parameter name written in the above < > must be the same as the parameter name passed in your def function. For example, if the parameter name I wrote is username, then the parameter name after login in my code must be username. In the above code, I passed two parameters, warrior and 123 respectively when I wrote the address.
3. Data type of URL1) If not specified, the default is string type
2) String: a string that accepts any character without a slash /.
3) int: integer
4) float: floating point type
5) path: similar to string, but can receive slashes/
6) uuid: receive only uuid strings
7) any: multiple paths can be specified
# coding: utf-8 from flask import Flask # Wei name__ Is the main file used to determine the operation of the flash app = Flask(__name__) # type: Flask app.debug = True # root directory @app.route('/') def hello_world(): return 'Hello World!' # @app.route('/content/<username>/<password>/') # def login(username, password): # return u 'my user name is:% s, password is:% s' (username, password) @app.route('/content/<int:username>/<int:password>/') def login(username, password): return u'My user name is:%s,The password is:%s' % (username, password) if __name__ == '__main__': app.run()
In the page, I pass in two 1s, and you can see:
But when I passed in warrior and 1, I couldn't find the page, because warrior is not of int type:
uuidNext, let's briefly talk about uuid, because uuid is used longer when passing parameters. uuid is the only string of characters and will never be repeated, for example:
# coding: utf-8 from flask import Flask import uuid app = Flask(__name__) # type: Flask app.debug = True # root directory @app.route('/') def hello_world(): return 'Hello World!' # @app.route('/content/<username>/<password>/') # def login(username, password): # return u 'my user name is:% s, password is:% s' (username, password) @app.route('/content/<uuid:username>/') def login(username): return u'My user name is:%s' % (username) print uuid.uuid4() if __name__ == '__main__': app.run()
In the above code, we first imported the import uuid module, and then through print uuid.uuid4() printed a string of UUIDs:
At this time, because we specified that username is uuid type, when we enter http://127.0.0.1 : the address will not be found when 5000 / content / 1 /. We will replace the uuid printed on the console with 1: http://127.0.0.1:5000/content/7bdcd04c-62fd-48e8-b12b-bbd636cd0315 / you can see:
anyAny is to specify any parameter to be passed. For example, if we specify the parameter as username or blog in the following code, then entering username or blog in the URL will link to the specified page, and entering any content page other than the specified parameter will report an error:
# coding: utf-8 from flask import Flask import flask import uuid app = Flask(__name__) # type: Flask app.debug = True # root directory @app.route('/') def hello_world(): return 'Hello World!' # @app.route('/content/<username>/<password>/') # def login(username, password): # return u 'my user name is:% s, password is:% s' (username, password) @app.route('/content/<uuid:username>/')def login(username): return u'My user name is:%s' % (username) @app.route('/post/<any(username,blog):name>/') def post_info(name): return u'id Yes:%s' % name print uuid.uuid4()if __name__ == '__main__': app.run()Parameter passing
Finally, let's talk about parameter passing. Theoretically, it is recommended that path -- receive strings with slashes to pass parameters, because this is conducive to the SEO of the website, that is, the ranking of the website in search engine results:
@app.route('/post/<path:username>/') def user(username): return u'The user name is:%s' % username
Another way to pass parameters is to query the string "path = 1 & username = warrior" used by most websites
@app.route('/post/')def question(): post_id = flask.request.args.get('post_id') return u'post_id Yes:%s' % post_id
above flask.request.args.get('post_id ') is to obtain the parameters in the address:
As for which way you want to use, it depends on whether you care about the ranking of your website in the search engine~
The content of this article is a little bit more. We will digest it slowly. Finally, we will post the code to you for your reference:
If for software testing, interface testing, automation testing, interview experience exchange. If you are interested, you can add software test exchange: 1085991341, and there will be technical exchanges with peers.
# coding: utf-8 from flask import Flask import flaskimport uuid # __name__It's for sure flask Running master file app = Flask(__name__) # type: Flask app.debug = True # root [email protected]('/') def hello_world(): return 'Hello World!' # @app.route('/content/<username>/<password>/') # def login(username, password): # return u'My user name is:%s,The password is:%s' % (username, password) # uuid @app.route('/content/<uuid:username>/') def login(username): return u'My user name is:%s' % (username) # any @app.route('/post/<any(username,blog):name>/') def post_info(name): return u'id Yes:%s' % name # [email protected]('/post/<path:username>/') def user(username): return u'The user name is:%s' % username # Pass parameters in the form of? Id = 1 & user = Warrior @app.route('/post/') def question(): post_id = flask.request.args.get('post_id') return u'post_id Yes:%s' % post_id print uuid.uuid4()if __name__ == '__main__': app.run()
The above content hopes to be helpful to you. Friends who have been helped are welcome to like and comment.