我正在学习如何使用Jinja2模板进行谷歌应用程序引擎。这是我编写的示例代码,但是当我运行它时它不会显示任何内容。
import os
import jinja2
import webapp2
from google.appengine.ext import db
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), extensions=['jinja2.ext.autoescape'], autoescape=True)
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_str(self, template, **params):
t = jinja_env.get_template(template)
return t.render(params)
def render(self, template, **kw):
self.write(self.render_str(template, **kw))
class login(db.Model):
user_fname=db.StringProperty(required=True)
user_lname=db.StringProperty(required=True)
user_name=db.StringProperty(required=True)
user_email=db.EmailProperty(required=True)
user_password=db.StringProperty(required=True)
repeat_password=db.StringProperty(required=True)
created=db.DateTimeProperty(auto_now_add=True)
class MainPage(Handler):
def get(self):
self.render("index.html")
def render_page(self,user_fname="",user_lname="",user_name="",user_email="",user_password="",repeat_password="",error="" ):
prof=db.GqlQuery("SELECT * FROM login")
self.render("index.html",user_fname=user_fname,
user_lname=user_lname,
user_name=user_name,
user_email=user_email,
user_password=user_password,
repeat_password=repeat_password,
error=error)
def post(self):
user_fname=self.request.get("user_fname")
user_name=self.request.get("user_name")
user_lname=self.request.get("user_lname")
user_email=self.request.get("user_email")
repeat_password=self.request.get("repeat_password")
user_password=self.request.get("user_password")
if (user_name and user_email and user_fname and user_lname and user_password and repeat_password):
if not(user_password==repeat_password):
error="passwords does not match check again carefull"
self.render_page(user_fname,user_lname,user_name,user_email,"","",error)
else:
#store in the database to be used later
a=login(
user_fname=user_fname,
user_lname=user_lname,
user_name=user_name,
user_email=user_email,
user_password=user_password,
repeat_password=repeat_password)
a.put()
self.redirect("/profile")
else:
error="please fill out the details above"
self.render_page(user_fname,user_lname,user_name,user_email,"","",error)
class Thankshandler(Handler):
def get(self):
self.render("profile.html")
app = webapp2.WSGIApplication([ ('/', MainPage),('/profile',Thankshandler)],debug=True)
并在profile.html中添加了
{% for next in prof %}
<ul>
<h1>{{next.user_name}}</hi>
</ul>
{% endfor %}
但它没有显示任何内容。我想知道如果有任何身体可以帮助请问有什么问题。 提前谢谢
答案 0 :(得分:1)