I can´t return the date and footer functions on flask I can´t return the date and footer functions on flask flask flask

I can´t return the date and footer functions on flask


The function is not returning anything because it is not called.

Where do you want the date/time to be returned ?

If it's in /index.html you should do something like that :

@app.route("/index.html/")def Home():    current_time = datetime.datetime.now()    return render_template("index.html", current_time=current_time)

And then in your template you can add the variable inside your HTML code like that for example:

Current Date and Time: {{ current_time }}}

Basically you can define variables values inside your python main script. Pass them as arguments to the render_template function and then use them inside your template with {{ your_var }}.

Have a look here : https://jinja.palletsprojects.com/en/2.11.x/templates/


I didn't have time either but I can look into it now.I think you are lost because you have two views with two different templates. And I guess, as for now, you only want to render one page.

So now your project should look like that:

Your mainscript.py is:

from flask import Flask, render_templatefrom datetime import datetimeapp = Flask("Timer Workout")@app.route("/")def Landing():    return render_template("Landing_Page.html")def footer():    footerdb = open("footer.txt")    for i in range (3):        footerdb.write("footer.txt" + " by Carlos ")    footerdb.close()    return render_template("Landing_Page.html", footerdb)@app.route("/index.html/")def Home():    current_time = datetime.datetime.now()    return render_template("index.html", current_time = current_time)if "Timer Workout" == "__main__":    app.run()

And then you should have two HTML files, Landing_page.html and index.html.

But, I think you are trying to render one and only one page with your footer and the current date and time.

For that I'll give you an example that should work. Just delete your footer() function which is buggy and not called anywhere and we'll make only one routing to render one template.

In your mainscript.py:

from flask import Flask, render_templatefrom datetime import datetimeapp = Flask("Timer Workout")@app.route("/")def home():    current_time = datetime.datetime.now()    footer = "Placeholder string just for debugging"    return render_template("index.html", current_time = current_time, footer = footer)if "Timer Workout" == "__main__":    app.run()

Then in your index.html file put something like that:

<html><head>    <title>Index page</title></head><body>Current date/time is : {{ current_time }}<br><br>The footer would be just below : <br>{{ footer }}</body></html>

Now when you access to yourwebsite.com/ you'll see index.html properly rendered.Of course then you should find a better way to add the footer, you will look into template inheritance with the extends tag. But for now try my example to understand the basics that should work easily.