tomzaragoza
tomzaragoza

Reputation: 13

Storing a string from a textbox to the Google App Engine Datastore

Is it possible to create a textbox in HTML, type a string into it, click a "save" button, store that information onto a GAE datastore model and have the text stay displayed in the textbox and saved in the datastore?

The HTML is on a separate file, just rendered through my main.py file using

class MainPage(webapp.RequestHandler):
def get(self):

    template_values = {}
    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path, template_values))

What I tried for my problem is this:

class equipmentBox(db.Model):
    equipCode = db.StringProperty()

class equipmentBoxGet(webapp.RequestHandler):
    def post(self):

Upvotes: 1

Views: 769

Answers (2)

Wooble
Wooble

Reputation: 89897

The best way to build this sort of interface, where the user "saves" data on the page without the page changing, is to use AJAX calls.

While a full explanation of how AJAX works is probably beyond the scope of an answer here, the basic idea is that you have a Javascript onclick event attached to your Save button, which sends the contents of your textbox via a POST request to the server. See the link above for a tutorial.

Upvotes: 2

Abdul Kader
Abdul Kader

Reputation: 5842

I think this will help, i have modified default guestbook app for you. Usual way of doing is having html files separately and using templates to render it. Here everything is just embedded into the controller itself

import cgi

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class EquipmentBox(db.Model):
      equipCode = db.StringProperty()


class MainPage(webapp.RequestHandler):
  def get(self):
    self.response.out.write('<html><body>')

    equips = db.GqlQuery("SELECT * FROM EquipmentBox")

    for equip in equips:

      self.response.out.write('<blockquote>%s</blockquote>' %
                              cgi.escape(equip.equipCode))

    # Write the submission form and the footer of the page
    self.response.out.write("""
          <form action="/post" method="post">
            <div><input type="text" name="equip_code" /></div>
            <div><input type="submit" value="post equipcode"></div>
          </form>
        </body>
      </html>""")

class EquipBox(webapp.RequestHandler):
  def post(self):
    equip = EquipmentBox()
    equip.equipCode = self.request.get('equip_code')
    equip.put()
    self.redirect('/')

application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                      ('/post', EquipBox)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()

Upvotes: 3

Related Questions