代码之家  ›  专栏  ›  技术社区  ›  MP32

我试图在HTML中显示python数据,结果并不是我所期望的

  •  0
  • MP32  · 技术社区  · 2 年前

    我的应用程序中有一个函数,它从数据库返回一个数字,我试图用flask在我的网页上显示它

    def handler():
        increment_visitor()
        return retrieve_visitor_count()
        
    @app.route('/')
    def home():
        handler()
        return render_template("index.html", count=handler)
    

    我将名称计数分配给处理程序,并尝试在索引中显示计数。html文件如下:

    <p>{{count}}</p>
    

    当我加载网页时,输出如下

    <function handler at 0x7f8c70069a60>
    

    当我打印处理程序函数时,它会输出适当的数字,那么如何使该数字正确显示在我的网页上呢?

    1 回复  |  直到 2 年前
        1
  •  1
  •   Raymi306    2 年前

    count 是处理程序,它是一个函数。添加括号以在模板中调用它,如下所示:

    {{count()}}
    

    但从你的逻辑来看,我认为你可能想要这样的东西:

    @app.route('/')
    def home():
        result = handler()
        return render_template("index.html", count=result)
    

    否则,您可能会将访客数增加两次