Model Couchdb
- From:
- Alasdair Macmillan
- Date:
- 2010-08-16 @ 17:23
Hi,
I am a total newbie to programming & python and wondered if anyone could
help me understand something I'm stuck with.
I want to use Couchdb with Flask and currently I am just trying to save a
form with username, password, email address and I want to save every
instance of that to Couchdb.
I have basically:
server = Server('http://localhost:5984')
database = 'harkdb'
class SignupForm(Form):
username = TextField('Username', [validators.Length(min=4, max=25)])
email = TextField('Email Address (will never be shared)',
[validators.Length(min=6, max=35)])
password = PasswordField('Password', [
validators.Required(),
validators.EqualTo('Re-enter password', message='Passwords must match')
])
confirm = PasswordField('Repeat Password')
accept_tos = BooleanField('I accept the TOS', [validators.Required()])
@app.route('/get-started/', methods=['GET', 'POST'])
def register():
form = SignupForm(request.form)
if request.method == 'POST' and form.validate():
user = User(form.username.data, form.email.data,
form.password.data)
db_session.add(user)
flash('Thanks for registering')
return redirect(url_for('loggedin.html'))
return render_template('get-started.html', form=form)
My couchdb server connection is working, and the database is set up and I
can save from the command line.
I am really learning programming in Python as well as Flask.
How do I make an instance of the class SignupForm when someone submits the
form (or is that automatic?)
How do I save that to Couchdb? I'm assuming I need to take variables and
insert them into somekind of dictionary that couchdb can save as a key +
value
The SignupForm class is a subclass (I'm presuming of WTForms 'Form') so I
also need to make it a sub class of Couchdb's 'Document' i.e class
SignupForm(Form, Document): The couchdb python module has field types.
Will they be the same as the WTForms ones?
Am I even using the right terminology?
I know this is beginner stuff but part of what attracted me to Flask as a
beginner is that I feel like I can get a handle on it more than Django
because it seems more transparent. But I have no one to ask these things!
Can anyone help me understand?
Kind thanks
Al Macmillan
Re: [flask] Model Couchdb
- From:
- Andrew Wilson
- Date:
- 2010-08-16 @ 20:27
On Mon, Aug 16, 2010 at 12:23 PM, Alasdair Macmillan <al@atomised.coop> wrote:
>
> How do I make an instance of the class SignupForm when someone submits
the form (or is that automatic?)
>
It looks like you have it set up correctly there. Your SignupForm
object is instantiated in your register view, in the line "form =
SignupForm(request.form)".
>
> How do I save that to Couchdb? I'm assuming I need to take variables and
insert them into somekind of dictionary that couchdb can save as a key +
value
>
You are on the right track. You need to take the data from the WTForms
form object and send it over to couchdb.
The place where you would do that is in the code block right under the
line "if request.method == 'POST' and form.validate():" That this
point, you have an object named form that contains the data that a
user has entered into the form on the webpage.
To be specific, the lines "user = User(form.username.data,
form.email.data, form.password.data)" and "db_session.add(user)" are
probably what you want to change. I'm not familiar with the python
couchdb bindings, so I can't help you much there.
The important thing is that form.username.data contains the SignupForm
username that was entered into the web form and form.email.data
contains the SignupForm email data and so on.
>
> The SignupForm class is a subclass (I'm presuming of WTForms 'Form') so
I also need to make it a sub class of Couchdb's 'Document' i.e class
SignupForm(Form, Document): The couchdb python module has field types.
Will they be the same as the WTForms ones?
>
The couchdb Document class is not compatible with the WTForms in this
way. You can think of WTForms as just a way of presenting a user
interface and validating that people are entering information that
makes sense. Doing whatever you want to do with that information (in
this case, you want to save it) is a separate matter entirely and you
do that somewhere else.
>
> Am I even using the right terminology?
>
The terminology is confusing here because the same words have
different meanings depending on the context. A field in WTForms is
different from a field in couchdb.
A field in WTForms is basically something that shows up on a web page
that visitors of your page will interact with - a text area or a
checkbox or something like that.
In couchdb, a field is a place where you store some type of
information. The python analogy is a key in a dict. But it's a
database so the data persists after your code stops running.
>
> I know this is beginner stuff but part of what attracted me to Flask as
a beginner is that I feel like I can get a handle on it more than Django
because it seems more transparent. But I have no one to ask these things!
>
No worries - there's a lot to take in. Keep reading docs and asking questions.
Re: [flask] Model Couchdb
- From:
- Dag Odenhall
- Date:
- 2010-08-16 @ 19:19
For starters, I suggest you try the extensions for CouchDB and WTForms.
CoudhDB: http://packages.python.org/Flask-CouchDB/
WTForms: http://packages.python.org/Flask-WTF/
Or maybe you prefer CouchDBKit for Couch; I'm not really familiar with
the differences.
CouchDBKit: http://packages.python.org/Flask-CouchDBKit/
Re: [flask] Model Couchdb
- From:
- Alasdair Macmillan
- Date:
- 2010-08-16 @ 19:49
Okay
Wondering if I'm on the right track:
@app.route('/get-started/', methods=['GET', 'POST'])
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 = db.save(create_user)
I'm trying to return the form contents as create_user to then save to couchdb.
However I'm getting an error: AttributeError: 'NoneType' object has no
attribute 'request'
Anybody able to help?
AL
Re: [flask] Model Couchdb
- From:
- Ron DuPlain
- Date:
- 2010-08-16 @ 19:58
On Mon, Aug 16, 2010 at 3:49 PM, Alasdair Macmillan <al@atomised.coop> wrote:
> I'm trying to return the form contents as create_user to then save to couchdb.
>
> However I'm getting an error: AttributeError: 'NoneType' object has no
attribute 'request'
How are you interacting with your app? In the browser or interactive
interpreter? I'm guessing that you are using the interpreter, where
you'll need a request context -- basically some code to to put your
interactive prompt inside what looks like a web request.
Flask-Script with `python manage.py shell` is one way to get a shell
with a request context.
http://packages.python.org/Flask-Script/
This gives you an interactive interpreter inside a `with
app.test_request_context()` block:
with app.test_request_context():
code.interact(...)
Does that help?
Ron