[Flask] Access form data in view function

Alex Hall ahall at autodist.com
Fri Apr 22 10:02:33 EDT 2016


Thanks. I've now seen this done two ways: do you access form data through
request.form, or through the form object you create and send when you
render? In your example, you used request.form, but I've seen it done like
this:

myForm = forms.LoginForm()
if request.method = get:
 return render_template("myLoginPage.html", form=myForm)
return myForm.username #as opposed to request.form.username

There's also use of the data property on form class members, but that, too,
is only used sometimes? I know what you were doing with the
request.form.get("name", None)--getting either the field content or
None--but when would you use the data property?

I'm still fighting with JS, so can't test on my own form to make sure I
have things back to normal. Hopefully I'll get this script worked out soon
(you wouldn't believe how hard it is to find a JS debugger that works with
screen readers; so far, I haven't done it). Thanks in advance for
clarification on the above questions. I think, once I have those down, I'll
understand form handling "for real".

On Fri, Apr 22, 2016 at 9:25 AM, Corey Boyle <coreybrett at gmail.com> wrote:

> @Alex - Not sure if you still need this or not. I put this example
> code together, let me know if it helps.
> https://dl.dropboxusercontent.com/u/3594910/random/myapp.py
>
> On Thu, Apr 21, 2016 at 6:59 PM, Alex Hall <ahall at autodist.com> wrote:
> > All,
> > Thanks for the help. It turns out that I missed something incredibly
> > obvious. The problem was that, days ago, I'd added a test to my JS to
> make
> > sure data was going from the form to the Python function. Instead of
> > serializing the form, I was sending just one value. I never removed that
> > test, and forgot I had it in there. I've removed it and adjusted the
> script,
> > and am now getting a 500 error, but I think once i find the scripting
> > problem that causes that I'll be in much better shape than i was. I've
> also
> > combined two functions into one, so I can access the form object created
> and
> > passed to the view instead of request. form. I didn't catch that I wasn't
> > supposed to use request.form until someone here pointed it out.
> >
> > Sent from my iPhone
> >
> > On Apr 21, 2016, at 16:29, Anthony Ford <ford.anthonyj at gmail.com> wrote:
> >
> > Can you upload your full app? Or at least a demo app with your view and
> your
> > template?
> >
> > Also, I think you are still confusing the form object created by WTForms
> > with the request.form object, which is completely different.
> >
> >
> > Anthony Ford,
> > KF5IBN,
> > ford.anthonyj at gmail.com
> >
> > On Thu, Apr 21, 2016 at 4:02 PM, Alex Hall <ahall at autodist.com> wrote:
> >>
> >> One more update: my form seems to only pass one field, no matter what. I
> >> made a little loop in the function that processes the form:
> >>
> >>  for i, j in request.form.iteritems():
> >>   print "form[{i}]={j}".format(i=i, j=j)
> >>
> >> This only ever prints my "orderNumber" text field, whether that field
> has
> >> a value or not. It never prints the other text field in the form, the
> >> checkbox, or either of the multiple select fields. I've been over the
> form
> >> template, html, and class again and again, trying to spot what I did
> >> differently for the orderNumber field that makes it stick when nothing
> else
> >> does. I've even reversed the order of fields in the form, to see if it's
> >> just the first field in the form that gets saved, but that didn't change
> >> anything. I'm really at a loss, and I've spent hours googling this to no
> >> avail. It makes no sense that the form would grab one field, but no
> others.
> >> I can't figure out what I could possibly be doing, and no one I could
> find
> >> has this problem. What code would it help to send to the list for you
> all to
> >> look at? Thanks.
> >>
> >> On Thu, Apr 21, 2016 at 8:13 AM, Alex Hall <ahall at autodist.com> wrote:
> >>>
> >>> Hello all,
> >>> Further to my question about multiple select fields yesterday, how
> does a
> >>> view function access form data in general? I know I use the global(?)
> >>> request object's "form" property, but beyond that I'm not sure. I can
> access
> >>> StringField values (I'm using WTF) by using request.form.fieldID. But
> single
> >>> select fields, multiple select fields, the status of checkboxes or
> radio
> >>> buttons, and so on I can't work out or find online. One resource said,
> for
> >>> checkboxes, to use request.form.getlist("checkboxID"), but that gave
> me an
> >>> empty list despite that checkbox being checked. What am I missing? I
> know
> >>> people usually use validators, but I need the values and statuses of
> >>> elements in my form-processing view function because I'm dynamically
> >>> constructing an SQL query based on the form's information.
> >>>
> >>> --
> >>> Alex Hall
> >>> Automatic Distributors, IT department
> >>> ahall at autodist.com
> >>
> >>
> >>
> >>
> >> --
> >> Alex Hall
> >> Automatic Distributors, IT department
> >> ahall at autodist.com
> >>
> >> _______________________________________________
> >> Flask mailing list
> >> Flask at python.org
> >> https://mail.python.org/mailman/listinfo/flask
> >>
> >
> >
> > _______________________________________________
> > Flask mailing list
> > Flask at python.org
> > https://mail.python.org/mailman/listinfo/flask
> >
>



-- 
Alex Hall
Automatic Distributors, IT department
ahall at autodist.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20160422/dbab65bf/attachment.html>


More information about the Flask mailing list