Danush Aaditya
Danush Aaditya

Reputation: 73

Double curly braces (Jinja2) not working in Flask

I am new to web development and I came across this very annoying issue.

Here is my app.py code:

from flask import Flask, render_template

app = Flask(__name__)

all_posts = [
    {
        'Title': 'Post 1.',
        'Content': 'Content in Post 1.'
    },
    {
        'Title': 'Post 2.',
        'Content': 'Content in Post 2.'
    }
]

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

The all_posts variable gets sent to posts.html.

Here is posts.html code:

{% extends 'base.html' %}

{% block head%} 
<title>Posts</title>
{% endblock %}

{% block body%} 
<h1>All Posts</h1>

    {% for post in posts %}

        <h2> {{post.title}} </h2>
        <p> {{post.content}} </p>

    {% endfor %}

{% endblock %}

But when I run /posts in the browser, nothing inside the double curly braces gets displayed. Any help is appreciated.

Upvotes: 0

Views: 1440

Answers (1)

Mohamed Feddad
Mohamed Feddad

Reputation: 234

Templates variables are case-sensitive. Try the following instead:

{% extends 'base.html' %}

{% block head%} 
<title>Posts</title>
{% endblock %}

{% block body%} 
<h1>All Posts</h1>

    {% for post in posts %}

        <h2> {{post.Title}} </h2>
        <p> {{post.Content}} </p>

    {% endfor %}

{% endblock %}

Upvotes: 1

Related Questions