Home > Mobile >  I'm trying to show python data in my HTML and the output is not what I expected
I'm trying to show python data in my HTML and the output is not what I expected

Time:06-26

Here's a function in my application that returns a number from a database and I'm trying to display it on my webpage using flask

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

I assign the name count to handler and try displaying count in my index.html file like so:

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

When I load my webpage, here's what the output is

<function handler at 0x7f8c70069a60>

When I print my handler function, it outputs the appropriate number, so how do I get that number to display on my webpage correctly?

CodePudding user response:

count is handler, which is a function. Add parentheses to call it in the template like so:

{{count()}}

Looking at your logic though, I think you may want something like this:

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

Otherwise you would presumably increment the visitor count twice

  • Related