Google App Engine 数据存储区 Jinja2 查询未显示
Google App Engine Datastore Jinja2 Query Not showing up
有什么问题吗?文章存储在 articleArchive 实体中,但我无法让它显示在我的 HTML 中。 html 中的 where id=article 是我希望显示查询的位置。谢谢!
Python代码:
import os
import jinja2
import webapp2
import time
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),
autoescape = True)
#CREATE DATABASE ENTITIY/CHART CLASS to store all articles, titles,
# subheadings, dates posted, author names, article ID, tags, etc.
class articleArchive(db.Model):
title = db.StringProperty(required = True)
article = db.TextProperty(required = True)
author = db.StringProperty(required = True)
datetime = db.DateTimeProperty(auto_now_add = 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 homePage(Handler):
def get(self):
self.render("homePage.html")
class submitPage(Handler):
def render_post(self, error="", title="", article="", author=""):
articles = db.GqlQuery("SELECT * FROM articleArchive ORDER BY datetime DESC")
self.render("submit.html", error=error, title=title, article=article, author=author)
def get(self):
self.render_post()
def post(self):
title = self.request.get("title")
article = self.request.get("article")
author = self.request.get("author")
if title and article and author:
a = articleArchive(title = title, author = author, article = article)
a.put()
self.redirect("/submit")
else:
error = "The title, author, and article fields must be filled out. "
self.render_post(error, title, article, author)
class singlePost(Handler):
def post(self):
self.render(submit.html)
app = webapp2.WSGIApplication([('/', homePage),
('/submit', submitPage),
('/id', singlePost)
],
debug=True)
HTML:
{% extends "base.html" %}
<!DOCTYPE html>
<html>
<head>
{% block head %}
<title>Submit a Post!</title>
<link type="text/css" rel="stylesheet" href="/stylesheets/submit.css">
{% endblock %}
</head>
<body>
{% block content %}
<h1>Article Submission</h1>
<form method="post" id="lol">
<label>
<div>Title</div>
<input type="text" name="title" value={{title}}>
</label>
<br>
<br>
<label>
<div>Author</div>
<input type="text" name="author" value={{author}}>
</label>
<br>
<br>
<label>
<div>Article</div>
<textarea type="text" name="article" form="lol" value={{article}}></textarea>
</label>
<div class="error">{{error}}</div>
<input type="submit">
</form>
<div id = "article">
{% for article in articles %}
<h1> {{article.title}} </h1>
<h2> {{article.author}} </h2>
<h3> {{article.datetime}} </h3>
<br>
<pre> {{article.article}} </pre>
<hr>
{% endfor %}
</div>
{% endblock %}
</body>
</html>
您的 render_post
函数正在查询并分配给 articles
但您正在将函数 kwarg
article
传递给您的 render
函数。这意味着在您的 jinja2 模板中,articles
始终未定义并默默地被忽略。
class submitPage(Handler):
def render_post(self, error="", title="", article="", author=""):
articles = db.GqlQuery("SELECT * FROM articleArchive ORDER BY datetime DESC")
self.render("submit.html", error=error, title=title, article=article, author=author)
有什么问题吗?文章存储在 articleArchive 实体中,但我无法让它显示在我的 HTML 中。 html 中的 where id=article 是我希望显示查询的位置。谢谢!
Python代码:
import os
import jinja2
import webapp2
import time
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),
autoescape = True)
#CREATE DATABASE ENTITIY/CHART CLASS to store all articles, titles,
# subheadings, dates posted, author names, article ID, tags, etc.
class articleArchive(db.Model):
title = db.StringProperty(required = True)
article = db.TextProperty(required = True)
author = db.StringProperty(required = True)
datetime = db.DateTimeProperty(auto_now_add = 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 homePage(Handler):
def get(self):
self.render("homePage.html")
class submitPage(Handler):
def render_post(self, error="", title="", article="", author=""):
articles = db.GqlQuery("SELECT * FROM articleArchive ORDER BY datetime DESC")
self.render("submit.html", error=error, title=title, article=article, author=author)
def get(self):
self.render_post()
def post(self):
title = self.request.get("title")
article = self.request.get("article")
author = self.request.get("author")
if title and article and author:
a = articleArchive(title = title, author = author, article = article)
a.put()
self.redirect("/submit")
else:
error = "The title, author, and article fields must be filled out. "
self.render_post(error, title, article, author)
class singlePost(Handler):
def post(self):
self.render(submit.html)
app = webapp2.WSGIApplication([('/', homePage),
('/submit', submitPage),
('/id', singlePost)
],
debug=True)
HTML:
{% extends "base.html" %}
<!DOCTYPE html>
<html>
<head>
{% block head %}
<title>Submit a Post!</title>
<link type="text/css" rel="stylesheet" href="/stylesheets/submit.css">
{% endblock %}
</head>
<body>
{% block content %}
<h1>Article Submission</h1>
<form method="post" id="lol">
<label>
<div>Title</div>
<input type="text" name="title" value={{title}}>
</label>
<br>
<br>
<label>
<div>Author</div>
<input type="text" name="author" value={{author}}>
</label>
<br>
<br>
<label>
<div>Article</div>
<textarea type="text" name="article" form="lol" value={{article}}></textarea>
</label>
<div class="error">{{error}}</div>
<input type="submit">
</form>
<div id = "article">
{% for article in articles %}
<h1> {{article.title}} </h1>
<h2> {{article.author}} </h2>
<h3> {{article.datetime}} </h3>
<br>
<pre> {{article.article}} </pre>
<hr>
{% endfor %}
</div>
{% endblock %}
</body>
</html>
您的 render_post
函数正在查询并分配给 articles
但您正在将函数 kwarg
article
传递给您的 render
函数。这意味着在您的 jinja2 模板中,articles
始终未定义并默默地被忽略。
class submitPage(Handler):
def render_post(self, error="", title="", article="", author=""):
articles = db.GqlQuery("SELECT * FROM articleArchive ORDER BY datetime DESC")
self.render("submit.html", error=error, title=title, article=article, author=author)