Beginner trying to get my head around how to save forms to Couchdb

3 views
Skip to first unread message

almacmillan

unread,
Aug 16, 2010, 3:12:11 PM8/16/10
to CouchDB-Python
Hi,

I am a complete beginner to programming & to python so please bear
with me. I have a signup form and I simply want to understand how to
save the posted form contents to a couchdb document. I am (using the
Flask framework with WTForms) and am trying to get my head around how
to do this. The only way I could think to do it was to return
create_user 's contents but it doesn't work and I don't understand.
Can anyone help me?

def register():
form = SignupForm(request.form)
if request.method == 'POST' and form.validate():
create_user = ({'username' : form.username.data, 'email' :
form.email.data,
'password': form.password.data})
flash('Thanks for registering')
return create_user, redirect(url_for('loggedin.html'))
return render_template('get-started.html', form=form)

create_user = register()
doc_id, doc_rev = db.save(create_user)

Martin Alderete

unread,
Aug 16, 2010, 4:36:45 PM8/16/10
to CouchDB-Python
Hello!
For add documents to the db you should use the "save" method in
Database class, sometimes you should commit the database too (if you
server doesn't auto commit).
Looking at your source code I found some mistakes with your data, you
have to send a valid dict object.
Your code should looks like:

def register():
form = SignupForm(request.form)
if request.method == 'POST' and form.validate():
#Creates a valid dictionary object
create_user_dict = {'username' : form.username.data, 'email' :
form.email.data, 'password': form.password.data}
#Save the doc in the db
doc_id, doc_rev = db.save(create_user_dict)
flash('Thanks for registering')
return create_user, redirect(url_for('loggedin.html'))

return render_template('get-started.html', form=form)


Cheers,

Martin
Reply all
Reply to author
Forward
0 new messages