繁体   English   中英

如何在Google App Engine中正确保存表单数据

[英]How to properly save form data in Google App Engine

我有一个电子邮件表单,试图将用户的电子邮件地址保存到数据库中。 该模型如下所示:

class EmailForm(db.Model):                                                
  email_address = db.EmailProperty

我一直在使用像这样的一些教程作为指南,其中表单中的数据是这样保存的:

title = form.title.data,
content = form.content.data

当我遵循相同的约定时

email = form.email_address.data

出现错误,EmailProperty没有数据属性。

我是Google App Engine的新手,但未在文档中找到答案。 谢谢!

您正在尝试将模型用作表单,这是两种不同的方式。

你还需要一步

from flaskext import wtf
from flaskext.wtf import validators

class EmailModel(db.Model):
    email_address = db.EmailProperty

class EmailForm(wtf.Form):
    email = wtf.TextField('Email Address', validators=[validators.Email()])

现在,在您看来,您可以像这样使用表格。

@app.route('/register', methods=['POST']
def register():
    form = EmailForm()
    if form.validate_on_submit():
        # This part saves the data from the form to the model.
        email_model = EmailModel(email_address = form.email.data)
        email.put()

我想这就是您要寻找的东西: https : //developers.google.com/appengine/docs/python/datastore/typesandpropertyclasses#Email

email_address = db.EmailProperty()

email_address = db.Email(“ larry@example.com”)

  1. 您正在使用旧的db类,而是使用ndb,只需将db替换为ndb( https://docs.google.com/document/d/1AefylbadN456_Z7BZOpZEXDq8cR8LYu7QgI7bt5V0Iw/edit?ndplr=1&pli=1

  2. 使用StringProperty代替EmailProperty,所​​以..

     class UserEmail(ndb.Model): email_address = ndb.StringProperty() 
  3. 要将模型放入数据存储区,请执行此操作

     user = UserEmail(email_address='foobar@gmail.com') user.put() 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM