Flask 简明教程

Flask – Variable Rules

通过在规则参数中添加可变部分,可以动态构建一个 URL。该可变部分被标记为 <variable-name> 。将它作为关键字参数传递给该规则关联的函数。

It is possible to build a URL dynamically, by adding variable parts to the rule parameter. This variable part is marked as <variable-name>. It is passed as a keyword argument to the function with which the rule is associated.

在以下的示例中, route() 装饰器的规则参数中包含 <name> 可变部分,连接至 URL ‘/hello’ 。因此,如果将 http://localhost:5000/hello/TutorialsPoint 输入为浏览器中的 URL ,那么 ‘TutorialPoint’ 将被作为参数提供给 hello() 函数。

In the following example, the rule parameter of route() decorator contains <name> variable part attached to URL ‘/hello’. Hence, if the http://localhost:5000/hello/TutorialsPoint is entered as a URL in the browser, ‘TutorialPoint’ will be supplied to hello() function as argument.

from flask import Flask
app = Flask(__name__)

@app.route('/hello/<name>')
def hello_name(name):
   return 'Hello %s!' % name

if __name__ == '__main__':
   app.run(debug = True)

保存上述脚本 hello.py ,接着从 Python shell 运行。接下来,打开浏览器,并输入 URL http://localhost:5000/hello/TutorialsPoint.

Save the above script as hello.py and run it from Python shell. Next, open the browser and enter URL http://localhost:5000/hello/TutorialsPoint.

浏览器中会显示以下输出。

The following output will be displayed in the browser.

Hello TutorialsPoint!

除了默认的字符串变量部分,还可以使用以下转换器构造规则 -

In addition to the default string variable part, rules can be constructed using the following converters −

Sr.No.

Converters & Description

1

int accepts integer

2

float For floating point value

3

path accepts slashes used as directory separator character

在以下代码中,将使用所有这些构造器。

In the following code, all these constructors are used.

from flask import Flask
app = Flask(__name__)

@app.route('/blog/<int:postID>')
def show_blog(postID):
   return 'Blog Number %d' % postID

@app.route('/rev/<float:revNo>')
def revision(revNo):
   return 'Revision Number %f' % revNo

if __name__ == '__main__':
   app.run()

从 Python Shell 运行上述代码。在浏览器中访问 URL http://localhost:5000/blog/11

Run the above code from Python Shell. Visit the URL http://localhost:5000/blog/11 in the browser.

给定的数字被用作 show_blog() 函数的参数。浏览器显示以下输出 -

The given number is used as argument to the show_blog() function. The browser displays the following output −

Blog Number 11

在浏览器中输入这个 URL - http://localhost:5000/rev/1.1

Enter this URL in the browser − http://localhost:5000/rev/1.1

revision() 函数将浮点数作为参数。浏览器窗口中会显示以下结果 -

The revision() function takes up the floating point number as argument. The following result appears in the browser window −

Revision Number 1.100000

Flask 的 URL 规则基于 Werkzeug’s 路由模块。这会确保形成的 URL 唯一并且基于 Apache 规定的优先级。

The URL rules of Flask are based on Werkzeug’s routing module. This ensures that the URLs formed are unique and based on precedents laid down by Apache.

考虑在以下脚本中定义的规则 −

Consider the rules defined in the following script −

from flask import Flask
app = Flask(__name__)

@app.route('/flask')
def hello_flask():
   return 'Hello Flask'

@app.route('/python/')
def hello_python():
   return 'Hello Python'

if __name__ == '__main__':
   app.run()

这两个规则看起来很相似,但第二个规则中使用了结尾斜杠 (/) 。结果,它变成了规范 URL。因此,使用 /python/python/ 会返回相同输出。然而,在第一个规则的情况下, /flask/ URL 会产生 404 Not Found 页面。

Both the rules appear similar but in the second rule, trailing slash (/) is used. As a result, it becomes a canonical URL. Hence, using /python or /python/ returns the same output. However, in case of the first rule, /flask/ URL results in 404 Not Found page.