Im following a flask tutorial from youtube, and I noticed when I run my code, my web application outputs nothing. This only happens when I'm trying to use HTML though.
Below is the code:
from flask import Flask, render_template, url_for
app = Flask(__name__)
posts = [
{
'author': 'Devo Developerr',
'title': 'Blog Post 1',
'content': 'First post content',
'date_posted': 'October 23, 2020'
},
{
'author': 'Jane Doe',
'title': 'Blog Post 2',
'content': 'Second post content',
'date_posted': 'April 21, 2020'
}
]
@app.route("/")
@app.route("/home")
def home():
return render_template('home.html', posts=posts)
@app.route("/about")
def about():
return render_template('about.html', title='About')
if __name__ == '__main__':
app.run(debug=True)
This is my main flask file.
This is my HTML file, home.html
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
{% for post in posts %}
<h1>{{ post.title }}</h1>
<p>By {{ post.author }} on {{ post.fate_posted }}</p>
<p>{{ post.content }}</p>
{% end for %}
</body>
</html>
What the aimed output is:
What the output is right now :
In your Flask project directory create the templates directory and move your templates there.
mkdir templates
mv *.html templates
Flask loads templates from this directory by default, you shouldn't need to configure anything else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With