将文本框中的字符串存储到Google App Engine数据存储区

|| 是否可以用HTML创建文本框,在其中输入字符串,单击“保存”按钮,将该信息存储到GAE数据存储模型中,并使文本保持显示在文本框中并保存在数据存储中? HTML位于单独的文件中,只需使用以下命令通过我的main.py文件呈现
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))
我为我的问题尝试的是:
class equipmentBox(db.Model):
    equipCode = db.StringProperty()

class equipmentBoxGet(webapp.RequestHandler):
    def post(self):
    
已邀请:
我认为这会有所帮助,我为您修改了默认留言簿应用程序。通常的做法是分别拥有html文件并使用模板来呈现它。在这里,一切都被嵌入到控制器本身中
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()
    
建立这种界面的最佳方法是使用AJAX调用,其中用户在页面上“保存”数据而不更改页面。 尽管对AJAX的工作原理的完整解释可能超出了此处的答案范围,但基本思想是您在Save按钮上附加了一个JavascriptSave3ѭ事件,该事件通过POST请求将文本框的内容发送到服务器。请参阅上面的链接以获取教程。     

要回复问题请先登录注册