如何使用 Python 在 Google App 引擎中显示 HTML 中的值?
我已经检索了这些值并将它们存储在一个变量中。
我尝试了以下方法,但它给了我一个错误。我想显示使用 for 循环检索到的记录,但不知道请帮忙。
代码:
import cgi
import webapp2
from google.appengine.ext import db
form="""<!DOCTYPE html>
<html>
<head>
<title>ascii</title>
</head>
<body>
<h1>/ascii</h1>
<form method="post">
<label>
<div>Title</div>
<input type="text" name="text" value="%(text)s">
</label>
<br>
<label>
Art
<br><textarea cols="50" rows="12" name="art" value="%(art)s"></textarea>
</label><br>
<input type="submit">
<div style="color:blue">%(error)s</div>
</form>
<hr>
<hr>
***{%for art in arts %}
<div>
<div>{% art.title %}</div>
<pre>{%art.art%}</pre>
</div>***
</body>
</html>"""
class Art(db.Model):
title = db.StringProperty(required=True)
art=db.TextProperty(required=True)
created=db.DateTimeProperty(auto_now_add=True)
class MainHandler(webapp2.RequestHandler):
def write_form(self,error="",text="",art="",arts=""):
***arts=db.GqlQuery("select * from Art order by created DESC")***
self.response.out.write(form %{"error":error,
"text":text,
"art":art)}
def escape_html(self,s):
return cgi.escape(s,quote=True)
def get(self):
self.write_form()
def post(self):
text1=self.escape_html(self.request.get("text"))
art1=self.escape_html(self.request.get("art"))
if text1 and art1:
a=Art(title=text1,art=art1)
a.put()
self.redirect("/")
else:
self.write_form("Both fields required",text1,art1)
app = webapp2.WSGIApplication([('/', MainHandler)],
debug=True)