Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jinja2.exceptions.TemplateSyntaxError: expected token 'end of print statement', got 'posted'

Tags:

python

html

flask

I'm new to programming and recently a friend of mine gave me a project to work on in order to get myself familiar with the programming environment and language (python in this particular example). https://www.youtube.com/watch?v=QnDWIZuWYW0 I used this video as a beginners tutorial to help me understand how i'm supposed to proceed with my personal project. the code i wrote is exactly how it is written in the video, and i get an error.

from flask import Flask, render_template
app = Flask(__name__)

posts = [
    {
        'author': 'Alon Salzmann',
        'title': 'First Post',
        'content': 'First post content',
        'date posted': 'September 5, 2018'
    },
    {
        'author': 'Alon Salzmann',
        'title': 'Second Post',
        'content': 'Second post content',
        'date posted': 'September 6, 2018'
    }
]

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

@app.route("/about")
def about():
    return render_template('About.html')

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

The code above is the python code I wrote, and the code below is the html code I wrote that involves python:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    {% for post in posts %}
    <h1>{{ post.title }}</h1>
    <p>By {{ post.author }} on {{ post.date posted }}</p>
    <p>{{ post.content }}</p>
    {% endfor %}
</body>
</html>

after running the program on both cmd and powershell (not simultaneously of course), and going to my localhost address, i got the error that appears in the title:

jinja2.exceptions.TemplateSyntaxError: expected token 'end of print statement', got 'posted'

I would love to get a good explanation as to why this has been happeninig, and how to fix this. Please remember I am a beginner and maybe need to be explained a few basic concepts at first :).

P.S. The guy's code in the video worked so I would love to understand where I went wrong.

like image 964
alon salzmann Avatar asked Sep 06 '18 13:09

alon salzmann


1 Answers

The line <p>By {{ post.author }} on {{ post.date posted }}</p> is the issue here

Replace it with <p>By {{ post.author }} on {{ post.date_posted }}</p>

post.date_posted is a variable and should be same as the one in your models, And spaces between them are obviously syntax errors(typos)

like image 57
Vineeth Sai Avatar answered Nov 12 '22 13:11

Vineeth Sai