📜  在 Flask 中生成动态 URL

📅  最后修改于: 2022-05-13 01:55:44.110000             🧑  作者: Mango

在 Flask 中生成动态 URL

先决条件: Flask 基础知识

创建应用程序时,对每个 URL 进行硬编码非常麻烦。解决此问题的更好方法是构建动态 URL。让我们先简单了解几个常用术语的含义。

  • 动态路由:获取URL中的动态数据(变量名),然后使用的过程。
  • 变量规则:可以通过用 标记部分来将可变部分添加到 URL。

让我们首先创建一个基本的烧瓶应用程序:

Python3
#importing the flask Module
from flask import Flask
  
# Flask constructor takes the name of
# current module (__name__) as argument
app = Flask(__name__)
  
@app.route('/')
# ‘/’ URL is bound with hello_world() function.
def home():
    return 'You are at home page.'
  
@app.route('/allow')
def allow():
    return 'You have been allowed to enter.'
  
@app.route('/disallow')
def disallow():
    return 'You have not been allowed to enter.'
  
# main driver function
if __name__ == '__main__':
    # run() method of Flask class runs the application
    # on the local development server.
    app.run()


Python3
#importing the flask Module
from flask import Flask
  
# Flask constructor takes the name of
# current module (__name__) as argument
app = Flask(__name__)
  
@app.route('/')
# ‘/’ URL is bound with hello_world() function.
def home():
    return 'You are at home page.'
  
# Use of  in the 
# route() decorator.
@app.route('/allow/')
def allow(Number):
    if Number < 25:
        return f'You have been allowed to enter beause your number is {str(Number)}'
    else:
       return f'You are not allowed'
  
# main driver function
if __name__ == '__main__':
    # run() method of Flask class runs the application
    # on the local development server.
    app.run()


输出:



现在考虑这样一种情况,您有很多用户,并且您希望将用户路由到在 URL 中包含他或她的姓名或 ID 以及模板的特定页面。如果您尝试手动执行此操作,则必须为每个用户手动键入完整的 URL。这样做可能非常乏味,几乎不可能。但是,这可以通过使用带有称为动态路由的东西的烧瓶来解决。

动态路由

我们现在将看看使用变量规则的更好方法。我们将为每个路由添加一个 。或者,我们还可以使用每个变量名称 定义转换器。默认情况下,转换器是String

例子:

@app.route('allow/')

OR

@app.route('allow/')

一些转换器是:

String(default) accepts any text without a slash
intaccepts positive integers
floataccepts positive floating point values
pathlike string but also accepts slashes
uuidaccepts UUID strings

让我们允许 ID 小于 25 的用户访问该页面。下面给出了带有动态 URL 绑定的修改后的代码。该函数使用在 route() 装饰器中传递的 作为参数。

@app.route('/allow/')
def allow(Number):
    if Number < 25:
        return f'You have been allowed to enter beause\
         your number is {str(Number)}'
    else:
       return f'You are not allowed'

蟒蛇3

#importing the flask Module
from flask import Flask
  
# Flask constructor takes the name of
# current module (__name__) as argument
app = Flask(__name__)
  
@app.route('/')
# ‘/’ URL is bound with hello_world() function.
def home():
    return 'You are at home page.'
  
# Use of  in the 
# route() decorator.
@app.route('/allow/')
def allow(Number):
    if Number < 25:
        return f'You have been allowed to enter beause your number is {str(Number)}'
    else:
       return f'You are not allowed'
  
# main driver function
if __name__ == '__main__':
    # run() method of Flask class runs the application
    # on the local development server.
    app.run()

输出: