Python Jinja模板代码显示为文本,而不是按预期工作
我目前正在使用Google Application Engine,Python作为编程语言,Jinja作为模板。问题是Jinja代码不起作用,它只是以文本的形式出现在网页上(就像它在标签中一样)。我正在使用Pycharm,我已经添加了Jinja作为我的模板languaje main.pyPython Jinja模板代码显示为文本,而不是按预期工作,python,google-app-engine,jinja2,Python,Google App Engine,Jinja2,我目前正在使用Google Application Engine,Python作为编程语言,Jinja作为模板。问题是Jinja代码不起作用,它只是以文本的形式出现在网页上(就像它在标签中一样)。我正在使用Pycharm,我已经添加了Jinja作为我的模板languaje main.py import os import webapp2 import jinja2 from google.appengine.ext import ndb JINJA_ENVIRONMENT = jinja2.E
import os
import webapp2
import jinja2
from google.appengine.ext import ndb
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
class MainHandler(webapp2.RequestHandler):
def get(self):
template_values = {
'user': "User",
'tuits': {"User", "Tweets this", "time"}
}
template = JINJA_ENVIRONMENT.get_template("index.html")
self.response.write(template.render(template_values))
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
index.html
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8" />
<title> TWITTER </title>
<script>
</script>
</head>
<body>
<h1>TUiter</h1>
<a href="/postTuit">Publicar TUit</a>
<div>
{% if tuits.count() > 0 %}
{% for tuit in tuits %}
<div>
<p>{{ tuit.user }}</p>
<p>{{ tuit.message }}</p>
<p>{{ tuit.time }}</p>
</div>
{% endfor %}
{% endif %}
</div>
</body>
</html>
你告诉应用引擎在你的
url://
指令中提供templates/index.html
逐字记录。那么我应该将文件重命名为其他文件吗?这是我第一次使用Jinja,尽管我检查了文档和所有东西,但我还是有点不知所措。不,我自己没有测试过,但我相信你应该简单地将操作更改为script:main.app
。在你的app.yaml:-url:/static\u文件:templates/index.html上传:index\.html中删除这些行。它们将显示索引html的静态版本。还有你的路线。*下面的main.app永远不会工作。@voscausa非常感谢,它工作了!在我下面的例子中,他们的app.yaml与我的完全相同,所以我从未想到它会来自于此,再次感谢!
application: supercoolname
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /
static_files: templates/index.html
upload: index\.html
- url: /postTuit
static_files: templates/postTuit.html
upload: postTuit\.html
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"