Reputation: 2000
I'm a newbie programmer, working on my first web app (using App Engine), so forgive my ignorance.
I have been modifying the Hello World program given by Google to try to make it do what I want.
In their demo (http://code.google.com/appengine/docs/python/gettingstarted/templates.html) they have 2 classes that create 2 pages - one is the index.html, and the other handles a form, by putting the form data into a DB, and then redirecting back to the main page, which loads info from the DB.
I would like to have a form on the main page where users submit a string, upon which various operations are performed, and the output is displayed on the main page.
The problem is that the data from the form submitted by users goes to the form handler page, and I can't figure out how to give the output back to the main page.
Upvotes: 3
Views: 2010
Reputation: 1128
I suggest you use an Ajax form, here is a link to a few examples (using jquery) jquery ajax form handling
Basically you define a form as you would do normally but instead of the post request sending you to another page, you post the request to a server handler which does something and passes back a response typically in json format which you can then process using javascript and update the main page with the results.
Upvotes: 0
Reputation: 74114
The natural way to persist the data among different submits is by using the Datastore.
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 hidden
fields between each web request.
Let's see an easy quick&dirty example: a web app that sums integers one by one.
application.py
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()
To pass the data to the index.html
view, the template.render function is used passing a dictionary variable that carries the result to the view:
template.render(path_to_the_view, template_values)
index.html
<html>
<body>
<form action="/sum" method="post">
<p>
<label for="value">Value to sum:
<input type="text" name="value"><br>
<input type="submit" value="Submit">
<input type="hidden" name="total" value="{{ total }}">
</p>
</form>
Last value submitted: {{ value }}| Total:{{ total }}
</body>
</html>
The index.html
view uses the dictionary keys value
and total
to show the result and to submit back the total to the controller using an hidden
field.
And here is the result:
Upvotes: 5
Reputation: 2978
If you are using webapp.requestHandler, request is a dictionary object which contains all the data submitted by user. So, here is how you get the data
class SubmitHandler (webapp.RequestHandler):
name = self.request.get ('name')
email = self.request.get ('emil')
# do whatever
This assumes that 'name' and 'email' are fields in your form.
Upvotes: 1
Reputation: 2829
I am not understanding why you want to have the output back to the mainpage. On submitting the form the control goes to the handler class. Now you can use this form data in your requesthandler class in any way you want it to.
And if you want some data to be transferred from this requesthandler to your mainpage you create global variables or create another form with hidden inputs to transfer data. Hope this will answer your question.
Upvotes: 1
Reputation: 16253
The HTML form generated by the index.html
template has the following form
tag:
<form action="/sign" method="post">
This means the form's data will be sent to /sign
when the user clicks the submit button. The action
URL determines which handler is used. First, we look in your app.yaml
file to see which script will be used to process the request:
handlers:
- url: /.*
script: helloworld.py
All URLs are handled by helloworld.py
, so lets look there:
application = webapp.WSGIApplication(
[('/', MainPage),
('/sign', Guestbook)],
debug=True)
Based on this, we can see that the /sign
URL is handled by the Guestbook
request handler. If you wanted the form submission to be handled by a different page you would either need to change the page the form is being submitted to (the action
attribute in the form
tag) or change the request handler assigned to the specified.
Upvotes: 1