已访问烧瓶打印网址

时间:2019-12-18 21:37:26

标签: python flask

我想创建一个函数,以打印访问的每个URL的名称。

我做了一个包装,可以像下面这样调用:

@url_print
@app.route('/')
def index():
    return render_template('index.html')

URL打印代码在这里:

def url_print(func, *args, **kwargs):
    print("Url '%s' was visited" % func.__name__)
    return(func(*args, **kwargs))

但是我不断收到以下错误:

ctx.app.update_template_context(context)
AttributeError: 'NoneType' object has no attribute 'app'

有人可以帮助我吗?预先感谢。

1 个答案:

答案 0 :(得分:1)

您发送的错误与这些行无关,请完全发送代码,但是更一般而言,如果要在python中打印函数名称,可以使用__name__

def function1(input):
   return input**2

print(function1.__name__)

这将打印:

function1