是否有可能创建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是如何工作的一个全面的解释可能是超出了这里的答案的范围,其基本思想是,你必须连接到您的保存按钮,它通过一个POST请求到服务器发送的文本框的内容,一个Javascript onclick
事件。见上面的教程链接。