0

#Flask Code

from flask import Flask, render_template

app = Flask(__name__)

posts = [
    {
        'author': 'User',
        'title': 'Test',
        'content': 'First post',
        'date_posted': '2021, 4 ,13',
    },

    {
        'author': 'User2',
        'title': 'Flask is cool',
        'content': 'Flask testing',
        'date_posted': '2021, 4 ,14'
    }
]

@app.route('/')
@app.route('/home')
def hello():
    return render_template('home.html', posts=posts)

#ignore this
@app.route('/about')
def about():
    return render_template('about.html')


if __name__ == '__main__':
    app.run(debug=True)

#HTML Code

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

    {% for post in posts %}
        <p>By {{ posts.author }} on {{ posts.date_posted }}</p>
        <p>By {{ post.content }}</p>
    {% endfor %}

</body>
</html>

the for loop is executing but the values in the dict. are not displaying, I am very new in flask so I'm pretty sure I have to add in some extra code..? Any help is appreciated :D

1 Answer 1

1

Use your loop variable post inside your for loop instead of posts.

{% for post in posts %}
<p>By {{ post.author }} on {{ post.date_posted }}</p>
<p>By {{ post.content }}</p>
{% endfor %}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.