URL and view function mapping

Today I’m talking about the mapping between URL and view function

URL and view function mapping

The mapping between url and view function is achieved through @app.route()decorators.

1. Only one slash represents the root directory-the home page.
# coding: utf-8
from flask import Flask
# __name__是用来确定flask运行的主文件
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()

The above code @app.route('/')decorator only has a slash /, and the running code will jump to the root directory-the home page. As shown:

2, transfer parameters

URL parameters are passed through <参数名称>the form. 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 yourself, the code is as follows:

# coding: utf-8
from flask import Flask
# __name__是用来确定flask运行的主文件
app = Flask(__name__)  # type: Flask
app.debug = True
# app.config.from_object('configs')
# 根目录
@app.route('/')
def hello_world():

    return 'Hello World!'
# 传参
@app.route('/content/<username>/<password>/')
def login(username, password):

    return u'我的用户名是:%s,密码是:%s' % (username, password)
if __name__ == '__main__':
    app.run()

After executing the code, enter the address you set in the browser to see:

It should be noted that the parameter name written in the above <> must be the same as the parameter name in your def function. For example, the parameter name I wrote is username, then the formal parameter name after login in my code must be username. Two parameters are passed, and warrior and 123 are passed respectively when writing the address.

3. URL data type

1) If not specified, the default is stringtype
2) string: string, accept any characters without slash /.
3) int: integer
4) float: floating point type
5) path: similar to string, but can receive slashes/
6) uuid: only accept uuid string
7) any: multiple paths can be specified

For example, now we specify a parameter of type int:

# coding: utf-8
from flask import Flask
# __name__是用来确定flask运行的主文件
app = Flask(__name__)  # type: Flask
app.debug = True

# 根目录
@app.route('/')
def hello_world():

    return 'Hello World!'
# @app.route('/content/<username>/<password>/')
# def login(username, password):
#     return u'我的用户名是:%s,密码是:%s' % (username,password)
@app.route('/content/<int:username>/<int:password>/')
def login(username, password):

    return u'我的用户名是:%s,密码是:%s' % (username, password)
if __name__ == '__main__':
    app.run()

In the page, I pass in two 1, you can see:

But when I pass in the warrior and 1, the page cannot be found, because the warrior is not of type int:

uuid

Next, let’s briefly talk about uuid, because uuid is more useful when passing parameters. Uuid is the only string of characters that will never be repeated, such as:

# coding: utf-8
from flask import Flask
import uuid
app = Flask(__name__)  # type: Flask
app.debug = True

# 根目录
@app.route('/')
def hello_world():

    return 'Hello World!'

# @app.route('/content/<username>/<password>/')
# def login(username, password):
#     return u'我的用户名是:%s,密码是:%s' % (username, password)

@app.route('/content/<uuid:username>/')
def login(username):

    return u'我的用户名是:%s' % (username)
print uuid.uuid4()
if __name__ == '__main__':
    app.run()

In the above code, we first imported the import uuidmodule, and then print uuid.uuid4()printed out a string of uuid:

At this time, because we specified that the username is of type uuid, when we enter http://127.0.0.1:5000/content/1/, we will not find the address. We replace the uuid printed on the console with 1: http://127.0.0.1:5000/content/7bdcd04c-62fd-48e8-b12b-bbd636cd0315/You can see:

any

Any is to specify any parameter to be passed. For example, in the following code we specify username or blog as the parameter, then we will link to the specified page if we enter username or blog in the URL, and enter the content page other than the specified parameter of any will report an error:

# coding: utf-8
from flask import Flask
import flask
import uuid

app = Flask(__name__)  # type: Flask
app.debug = True

# 根目录
@app.route('/')
def hello_world():

    return 'Hello World!'

# @app.route('/content/<username>/<password>/')
# def login(username, password):
#     return u'我的用户名是:%s,密码是:%s' % (username, password)

@app.route('/content/<uuid:username>/')def login(username):

    return u'我的用户名是:%s' % (username)
 @app.route('/post/<any(username,blog):name>/')
def post_info(name):

    return u'id是:%s' % name
print uuid.uuid4()if __name__ == '__main__':
    app.run()

Parameter passing

Finally, let's talk about parameter passing. Theoretically recommend path-receiving parameters in the form of a string with slashes, because this is beneficial to the website’s SEO, that is, the website’s ranking in search engine results:

@app.route('/post/<path:username>/')
def user(username):

    return u'用户名字是:%s' % username


Another way to pass parameters is the ?path=1&username=warriorquery string method used by most websites :

@app.route('/post/')def question():

    post_id = flask.request.args.get('post_id')
    return u'post_id是:%s' % post_id

The above flask.request.args.get('post_id')is to get the parameters in the address:

As for which method you want to use, it depends on whether you care about your website’s ranking in search engines~

The content of this article is a little bit too much, we will digest it slowly, and finally post the code for your reference:
how to exchange experience on software testing, interface testing, automated testing, and interview. If you are interested, you can add software test communication: 1085991341, and there will be technical exchanges with colleagues.

# coding: utf-8
from flask import Flask
import flaskimport uuid

# __name__是用来确定flask运行的主文件
app = Flask(__name__)  # type: Flask
app.debug = True

# 根目录@app.route('/')
def hello_world():

    return 'Hello World!'

# @app.route('/content/<username>/<password>/')
# def login(username, password):

#     return u'我的用户名是:%s,密码是:%s' % (username, password)
# uuid
@app.route('/content/<uuid:username>/')
def login(username):

    return u'我的用户名是:%s' % (username)
# any
@app.route('/post/<any(username,blog):name>/')
def post_info(name):

    return u'id是:%s' % name
# [email protected]('/post/<path:username>/')
def user(username):

    return u'用户名字是:%s' % username
# ?id=1&user=warrior形式传递参数
@app.route('/post/')
def question():

    post_id = flask.request.args.get('post_id')
    return u'post_id是:%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.

Guess you like

Origin blog.csdn.net/Chaqian/article/details/106433140