Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The natural way to persist the data among different submits is by using the <a href="http://code.google.com/appengine/docs/python/gettingstarted/usingdatastore.html" rel="noreferrer">Datastore</a>.<br> If, for exercise, you don't want to use the datastore, you are forced to pass the data to the view and read it back using some <code>hidden</code> fields between each web request.</p> <p>Let's see an easy <em>quick&amp;dirty</em> example: a web app that sums integers one by one.</p> <p><strong>application.py</strong></p> <pre><code>import os from google.appengine.ext import webapp from google.appengine.ext.webapp import template from google.appengine.ext.webapp.util import run_wsgi_app class Sum(webapp.RequestHandler): def get(self): path = os.path.join(os.path.dirname(__file__), 'index.html') template_values = {"total":0} self.response.out.write(template.render(path, template_values)) def post(self): value = self.request.get("value") total = self.request.get("total") total = int(total) + int(value) path = os.path.join(os.path.dirname(__file__), 'index.html') template_values = {"value":value, "total":total } self.response.out.write(template.render(path, template_values)) application = webapp.WSGIApplication( [('/', Sum), ('/sum', Sum)], debug=True) def main(): run_wsgi_app(application) if __name__ == "__main__": main() </code></pre> <p>To pass the data to the <code>index.html</code> view, the template.render function is used passing a dictionary variable that carries the result to the view:</p> <pre><code>template.render(path_to_the_view, template_values) </code></pre> <p><strong>index.html</strong></p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;form action="/sum" method="post"&gt; &lt;p&gt; &lt;label for="value"&gt;Value to sum: &lt;input type="text" name="value"&gt;&lt;br&gt; &lt;input type="submit" value="Submit"&gt; &lt;input type="hidden" name="total" value="{{ total }}"&gt; &lt;/p&gt; &lt;/form&gt; Last value submitted: {{ value }}| Total:{{ total }} &lt;/body&gt; &lt;/html&gt; </code></pre> <p>The <code>index.html</code> view uses the dictionary keys <code>value</code> and <code>total</code> to show the result and to submit back the total to the controller using an <code>hidden</code> field.</p> <p>And here is the result:</p> <p><img src="https://i.stack.imgur.com/qbySR.png" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload