Anish Kothari
Anish Kothari

Reputation: 87

How to properly save form data in Google App Engine

I have an email form where I'm trying to save the user's email address to the database. The model looks like this:

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

I've been using a few tutorials like this as a guide where the data from the form is saved like this:

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

when I follow the same convention, writing

email = form.email_address.data

there is an error that the EmailProperty does not have a data attribute.

I'm new to Google App Engine but I haven't found an answer in the docs. Thanks!

Upvotes: 1

Views: 229

Answers (3)

  1. You are using the old db class, use instead ndb, just replace db for ndb (https://docs.google.com/document/d/1AefylbadN456_Z7BZOpZEXDq8cR8LYu7QgI7bt5V0Iw/edit?ndplr=1&pli=1)

  2. Use StringProperty instead EmailProperty, so..

    class UserEmail(ndb.Model):                                                
      email_address = ndb.StringProperty()
    
  3. To put models in datastore, do this

    user = UserEmail(email_address='[email protected]')
    user.put()
    

Upvotes: 0

Michael Davis
Michael Davis

Reputation: 2430

You are attempting to use a Model as a Form, which are two different things.

You need another step

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()])

Now, in your view you can use the form like so.

@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()

Upvotes: 5

Gaby Solis
Gaby Solis

Reputation: 2587

I guess this is what you are looking for: https://developers.google.com/appengine/docs/python/datastore/typesandpropertyclasses#Email

email_address = db.EmailProperty()

email_address = db.Email("[email protected]")

Upvotes: 0

Related Questions