【问题标题】:Trying to pass an argument to a function, can get through the ''@app.route'' in python flask尝试将参数传递给函数,可以通过 python 烧瓶中的 ''@app.route''
【发布时间】:2021-06-15 17:22:01
【问题描述】:

我正在尝试将参数传递给我在 app.route 中创建的函数。

在下面的代码中,我创建了 2 个页面,1 个主页和 1 个管理页面。 我想允许或拒绝访问 /admin 页面,具体取决于我是否直接在我的 python 代码中将参数 ADMINpass 设置为 False 或 True。 我知道在这种特殊情况下,它会起作用,因为我不需要传递参数,但是如果我在另一个下标中定义我的管理页面并将其导入主脚本,它会告诉我 ''admin 正好 1参数(给定 0)。所以我希望这个功能适用于任何情况。

我现在并不真正担心安全性,因为我正在测试一些东西。 非常感谢,如果已经发布了类似的问题,我们深表歉意。代码如下:

###User section, importing parameters
ADMINpass=False
###End of user section

##Main code
@app.route('/') #Home page
def homepage():
   return 'Hello, main page here'


@app.route('/admin') #Admin page that I want to access
def admin(ADMINpass):  #Trying to pass the argument here will not work, probably because of the app.route
   if ADMINpass==False:    #If admin not activated in code, redirect to homepage
      a=redirect(url_for('homepage'))  
   if ADMINpass == True:  #If admin is activated, permit access
      print('This is the admin page')

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

【问题讨论】:

  • app.run() 末尾没有括号,这意味着该函数永远不会被调用。
  • 嘿 Jasmijn,谢谢,但我确实将它包含在我的代码中,只是忘记在这里写了,我要编辑我的文本。谢谢指出
  • “用户部分”是什么意思?那部分是否与管理员路由属于同一模块?

标签: python flask


【解决方案1】:

这里有几个问题。

@app.route 装饰器是 admin() 函数的包装器。它提供了路由注册的额外功能(允许“/admin”字符串作为端点的东西)、请求处理(您可以使用它指定像“POST”这样的 HTTP 方法)等等(参见Flask docs for route()) .

如果你这样做了:

@app.route('/admin') #Admin page that I want to access
def admin(ADMINpass):  #Trying to pass the argument here will not work, probably because of the app.route

Flask 希望您使用它来参数化端点本身:

@app.route('/endpoint/<something>')
def func(something):
    return something
$ curl localhost:5000/endpoint/value_of_something
value_of_something

我建议Miguel Grinberg's guide to decorators 了解有关装饰器的更多信息,重点是 Flask。

回到您的问题,您可以这样做(但请继续阅读以了解为什么我认为您不应该这样做):

ADMINpass=False
@app.route('/endpoint')
def admin():
  global ADMINpass
  return f'ADMINpass is {ADMINpass}'

全局变量的问题在于它们可以在其他代码中的任何时候更改。这通常是在代码中引入错误并降低代码可维护性和测试难度的好方法。

对于服务器,问题更为根本 - 如果您的代码看起来像这样怎么办?

ADMINpass=False
@app.route('/endpoint')
def admin():
    global ADMINpass
    ADMINpass = not ADMINpass
    return f'ADMINpass is {ADMINpass}'

每个请求(可能来自不同的客户端)都会改变它,所以它的价值几乎是不可能的:

$ curl localhost:5000/endpoint
ADMINpass is False
$ curl localhost:5000/endpoint
ADMINpass is True
$ curl localhost:5000/endpoint
ADMINpass is False

您可能正在寻找Flask session objects。存储在会话对象中的值特定于每个客户端,因为它的实现基于 cookie。如果这是您要实现的身份验证机制实现,那么这可能正是您所需要的。

from flask import session
app.something = 'value_of_something_else'
@app.route('/endpoint')
def admin():
   return app.something
$ curl localhost:5000/endpoint
value_of_something_else

如果您需要单个静态值,克服尝试使用该变量时可能遇到的错误的一种方法是使用您可以随时调用的函数:

def admin_pass():
    return False
@app.route('/endpoint')
def admin():
    if not admin_pass():
        ...

【讨论】:

    猜你喜欢
    • 2017-02-21
    • 2018-11-04
    • 1970-01-01
    • 2018-02-17
    • 2018-10-08
    • 2011-02-20
    • 2021-04-10
    • 1970-01-01
    • 2021-12-13
    相关资源
    最近更新 更多