jinja2.exceptions.TemplateSyntaxError:预期标记“打印语句结束",已“发布" [英] jinja2.exceptions.TemplateSyntaxError: expected token 'end of print statement', got 'posted'

查看:129
本文介绍了jinja2.exceptions.TemplateSyntaxError:预期标记“打印语句结束",已“发布"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程的新手,最近我的一个朋友给了我一个项目,以使自己熟悉编程环境和语言(在此特定示例中为python). https://www.youtube.com/watch?v=QnDWIZuWYW0 我使用了此视频作为初学者教程,可以帮助我了解如何进行我的个人项目.我编写的代码与视频中的代码完全相同,但出现错误.

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)

上面的代码是我编写的python代码,下面的代码是我编写的涉及python的html代码:

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>

在cmd和powershell上都运行程序之后(当然不是同时运行),并转到我的本地主机地址,我得到了标题中出现的错误:

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:预期标记打印语句结束",被发布"了""

"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 :).

ps.视频中那个家伙的代码有效,所以我很想了解我哪里出错了.

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

推荐答案

行{code>< p>由{{post.author}}于{{post.date posted}}</p> 是这里的问题

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

在{{post.date_posted}}</p>

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

post.date_posted 是一个变量,应该与模型中的变量相同,并且它们之间的空格显然是语法错误(typos)

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)

这篇关于jinja2.exceptions.TemplateSyntaxError:预期标记“打印语句结束",已“发布"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆