Reputation: 375
How can I display the values in HTML in Google App engine using Python?
I have retrieved the values and have stored them in a variable.
I tried the following but it gave me an error.I would like to display the records retrieved using a for loop but have no idea please help.
Code:
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)
Upvotes: 0
Views: 100
Reputation: 21835
This method is not scaling very well and it's not the right approach. Check out how the templates are being used in the Getting Started tutorial for Python on Google App Engine.
The general idea is that you are writing your HTML
in a template and then by passing some parameters to it, you will be able to user for
-loops, if
-statements and many more.
Upvotes: 2