librelist archives

« back to archive

Populating TextField from Database

Populating TextField from Database

From:
Gary Chambers
Date:
2013-09-09 @ 15:08
All,

I'm trying to pre-populate a TextField from session data (or from a
database, entirely without SQLAlchemy).  Once the form field is loaded, it
seems to be a permanent value regardless of what is submitted through the
form.  According to what I've read, the loading of the form data needs to be
done in the view, but not even that works for me.  For brevity, I'll use
session data in my example:

# Form
class ProfileForm(form):
     rank_title = TextField('Rank/Title')
     lastname = TextField('Last Name', [Required()])

     # This doesn't work, and is commented-out when trying
     # accomplish the same in the view.
     def __init__(self, *args, **kwargs):
         Form.__init__(self, *args, **kwargs)
         self.rank_title = session['rank_title']
         self.lastname = session['lastname']


# View
@app.route('/profile', methods=['GET', 'POST']):
def profile():
     # Unfortunately, this doesn't work, either, and is also
     # commented-out when trying to accomplish it in the form.
     form = ProfileForm(request.form)
     form.rank_title = session['rank_title']
     form.lastname = session['lastname']

     if request.method == 'POST' and form.validate():
         update_db_from_validated_POST_data()
         update_session_data_too()
         flash('Profile updated')
         return redirect(url_for('profile'))

     ...

I cannot determine what I'm doing wrong as the results are identical using
either method.  I'm using Python 2.7.5 with:

Flask==0.10.1
Flask-WTF==0.8.3

Thank you in advance.

--
G.

Re: [flask] Populating TextField from Database

From:
Anthony Ford
Date:
2013-09-09 @ 16:06
On Mon, Sep 9, 2013 at 11:08 AM, Gary Chambers <gwchamb@gwcmail.com> wrote:

> class ProfileForm(form):
>      rank_title = TextField('Rank/Title')
>

There is a possibility that this is a transcription error going from your
code to the email, but the object from which you are inheriting is 'Form'
not 'form'. I.e. it should be

  class ProfileForm(Form)
     rank_title = TextField('Rank/Title')
     ...

Good Luck,

Anthony Ford,
KF5IBN,
ford.anthonyj@gmail.com

Re: [flask] Populating TextField from Database

From:
Ryan O'Donnell
Date:
2013-09-09 @ 16:38
form.rank_title = session['rank_title']
form.lastname = session['lastname']

I'm pretty sure you want to set the data to those values, correct?

Try: form.rank_title*.data*, etc..

Also, Anthony is right: the class you're trying to inherit from is "Form"
not "form". "from wtforms import Form" I believe, without checking.


On Mon, Sep 9, 2013 at 12:06 PM, Anthony Ford <ford.anthonyj@gmail.com>wrote:

>
> On Mon, Sep 9, 2013 at 11:08 AM, Gary Chambers <gwchamb@gwcmail.com>wrote:
>
>> class ProfileForm(form):
>>      rank_title = TextField('Rank/Title')
>>
>
> There is a possibility that this is a transcription error going from your
> code to the email, but the object from which you are inheriting is 'Form'
> not 'form'. I.e. it should be
>
>   class ProfileForm(Form)
>      rank_title = TextField('Rank/Title')
>       ...
>
> Good Luck,
>
> Anthony Ford,
> KF5IBN,
> ford.anthonyj@gmail.com
>

Re: [flask] Populating TextField from Database

From:
Gary Chambers
Date:
2013-09-09 @ 17:44
All,

> form.rank_title = session['rank_title']
> form.lastname = session['lastname']
> 
> Try: form.rank_title.data, etc..
> 
> There is a possibility that this is a transcription error going from your
> code to the email, but the object from which you are inheriting is 'Form'
> not 'form'. I.e. it should be
>
>   class ProfileForm(Form) rank_title = TextField('Rank/Title') ...

Those are typos, thanks for pointing them out, and I apologize for the lack
of attention to detail in my request for assistance.  I was attempting to be
as concise as possible.

My application runs without any syntax errors.  The only problem is that the
data coming from the browser via POST is either ignored or overridden (with
the values that were initially set from the session) somewhere.  Any ideas
on why this is occurring will be appreciated.

--
G.

Re: [flask] Populating TextField from Database

From:
Ryan O'Donnell
Date:
2013-09-09 @ 17:51
I think this one is easy...

Put:

form.rank_title = session['rank_title']
form.lastname = session['lastname']

inside:

if not request.method == 'POST':

Let me know if that works :)


On Mon, Sep 9, 2013 at 1:44 PM, Gary Chambers <gwchamb@gwcmail.com> wrote:

> All,
>
> > form.rank_title = session['rank_title']
> > form.lastname = session['lastname']
> >
> > Try: form.rank_title.data, etc..
> >
> > There is a possibility that this is a transcription error going from your
> > code to the email, but the object from which you are inheriting is 'Form'
> > not 'form'. I.e. it should be
> >
> >   class ProfileForm(Form) rank_title = TextField('Rank/Title') ...
>
> Those are typos, thanks for pointing them out, and I apologize for the lack
> of attention to detail in my request for assistance.  I was attempting to
> be
> as concise as possible.
>
> My application runs without any syntax errors.  The only problem is that
> the
> data coming from the browser via POST is either ignored or overridden (with
> the values that were initially set from the session) somewhere.  Any ideas
> on why this is occurring will be appreciated.
>
> --
> G.
>

Re: [flask] Populating TextField from Database

From:
Gary Chambers
Date:
2013-09-10 @ 00:36
All,

> the "data" method of your field is just a dictionary containing the 
values. What's happening is something along the lines of this:
> 
> 1) You set the values of the forms
> 2) They're displayed
> 3) They get changed by the user
> 4) The data gets sent back VIA POST
> 5) WTForms puts this POST, as a dictionary, in the "data" method of your field
> 6) Your view promptly overwrites that
> 
> See? It's not immutable - you're just overwriting it before you get to 
access the data you want.

My error was a lack of attention to detail, attributable (though not as 
much) to my lack of familiarity with Python and Flask.  As Ryan 
pointed-out, I was overwriting form data by setting the defaults without 
first checking to see if request.method == 'POST'.  Simply, the solution 
was:

@app.route('/profile/', methods=('GET', 'POST'))
def profile():
	form = ProfileForm(request.form)
	if not request.method == 'POST':
		intialize_form_from_session()

	if request.method == 'POST' and form.validate():
		update_session_from_form()

	...

Thanks to Ryan O'Donnell and Anthony Ford for their input and assistance 
with this.  I guess I'm pretty much alone in using this pattern in my view
since even simple examples of its application weren't returned in search 
engine hits.

--
G.

Re: [flask] Populating TextField from Database

From:
Anthony Ford
Date:
2013-09-09 @ 18:12
If you are using Flask-WTF, you can specify a secondary obj to fetch data
from in the event data is not available in request.form. This is achieved
via the obj attribute in the form instantiation, i.e.

form = ProfileForm(obj=userObj)

When using Flask-WTF, it assumed that the form data is coming from request.form
and as such, it is not needed.

Specifying a DB object as the obj attribute will not override the form, as
it has precedence. It does however require an object with attributes, and
doesn't take a dict. This works well if you use an ORM, but if you are
directly fetching your DB results, it's a bit trickier.



Anthony Ford,
KF5IBN,
ford.anthonyj@gmail.com


On Mon, Sep 9, 2013 at 1:44 PM, Gary Chambers <gwchamb@gwcmail.com> wrote:

> All,
>
> > form.rank_title = session['rank_title']
> > form.lastname = session['lastname']
> >
> > Try: form.rank_title.data, etc..
> >
> > There is a possibility that this is a transcription error going from your
> > code to the email, but the object from which you are inheriting is 'Form'
> > not 'form'. I.e. it should be
> >
> >   class ProfileForm(Form) rank_title = TextField('Rank/Title') ...
>
> Those are typos, thanks for pointing them out, and I apologize for the lack
> of attention to detail in my request for assistance.  I was attempting to
> be
> as concise as possible.
>
> My application runs without any syntax errors.  The only problem is that
> the
> data coming from the browser via POST is either ignored or overridden (with
> the values that were initially set from the session) somewhere.  Any ideas
> on why this is occurring will be appreciated.
>
> --
> G.
>