Reputation: 151
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.
Upvotes: 13
Views: 94364
Reputation: 3447
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>
Upvotes: 19
Reputation: 1046
For this error message, in my case, there was an extra curly bracket character, close to a Jinja fragment call. Neither online Jinja template parsers, nor plugins, caught this - they didn't provide a meaningful error message. Had to go through line by line to see where the issue was.
Error message, when parsing the template (with the syntax error) in Python:
expected token 'end of print statement', got ':' jinja
Upvotes: 1
Reputation: 21
Also happen to me, in other case where you got the same error but your syntaxes was right, I replace:
{{ mycode }}
with where it's not print something but do something:
{% mycode %}
And it's work.
{{ }} is used to print
{% %} is used to do instruction (for, if, ...)
Upvotes: 1
Reputation: 807
You have to do <p>By {{ post['author'] }} on {{ post['date posted'] }}
to make it work. it is a dict. So you have to get the values with indicies instead of calls.
Upvotes: 0