Reputation: 4049
so i have made a form in WTForms with an edit button which allows the user to edit the previous data in the form.
The problem i have is fetching the new data from the form.when i do request.form i get the following:
ImmutableMultiDict([('csrf_token', u'20130702225444##3f1c28cecaf55dc0e441d9820dfb52bb6df3d200'), ('csrf_token', u'20130702225444##3f1c28cecaf55dc0e441d9820dfb52bb6df3d200'), ('csrf_token', u'20130702225444##3f1c28cecaf55dc0e441d9820dfb52bb6df3d200'), ('csrf_token', u'20130702225444##3f1c28cecaf55dc0e441d9820dfb52bb6df3d200'), ('location_name', u'b'), ('feed_url', u'bkj'), ('title', u'b'), ('url', u'b'), ('date_crawled', u'b'), ('content_url', u'b'), ('longitude', u'b'), ('latitude', u'b'), ('date_added', u'b'), ('types', u'b')])
what i want is to retrieve the following from the above output
('location_name', u'b'), ('feed_url', u'bkj'), ('title', u'b'), ('url', u'b'), ('date_crawled', u'b'), ('content_url', u'b'), ('longitude', u'b'), ('latitude', u'b'), ('date_added', u'b'), ('types', u'b')])
Upvotes: 0
Views: 520
Reputation: 16336
You already did the work of writing out a form class in WTForms (and I suspect Flask-WTF, given that the crsf_token would not be there normally unless you're using CSRF on purpose).
so that means you already did something akin to:
class MyForm(Form):
feed_url = TextField(...)
# etc
and then you already did something like
def my_view():
form = MyForm(request.form)
render('mytemplate.html', form=form)
Now that you have this awesome form object, use it! That is, access the data off either form.data
or form.feed_url.data
which contains datatype-coerced data. Furthermore, you can use the validation logic in wtforms to make sure you don't have any bad data. There's no reason to use request.form
which is the raw input coming from your framework.
This will land you with something like (note this is a generic example for an imaginary pseudo-framework, you will need to get the appropriate call names for your framework):
def edit_location(location_id):
my_object = LocationInfo.get(location_id)
form = MyForm(request.form, obj=my_object)
if request.form and form.validate():
# If we got here, we have POST data and the form validated.
form.populate_obj(my_object) # Super cool magic!
my_object.save()
return redirect(...)
# If we fall back to here, it means validation failed or we're
# viewing the form for the first time.
render('mytemplate.html', form=form)
I recommend you read the Crash Course for WTForms and also some of the other docs on there for a better idea of how to use WTForms.
Upvotes: 1
Reputation: 13699
You can use a list comprehension to filter out the csrf_tokens.
>>> import werkzeug
>>> i = werkzeug.ImmutableMultiDict([('csrf_token', u'20130702225444##3f1c28cecaf55dc0e441d9820dfb52bb6df3d200'), ('csrf_token', u'20130702225444##3f1c28cecaf55dc0e441d9820dfb52bb6df3d200'), ('csrf_token', u'20130702225444##3f1c28cecaf55dc0e441d9820dfb52bb6df3d200'), ('csrf_token', u'20130702225444##3f1c28cecaf55dc0e441d9820dfb52bb6df3d200'), ('location_name', u'b'), ('feed_url', u'bkj'), ('title', u'b'), ('url', u'b'), ('date_crawled', u'b'), ('content_url', u'b'), ('longitude', u'b'), ('latitude', u'b'), ('date_added', u'b'), ('types', u'b')])
>>> i
ImmutableMultiDict([('csrf_token', u'20130702225444##3f1c28cecaf55dc0e441d9820dfb52bb6df3d200'), ('csrf_token', u'20130702225444##3f1c28cecaf55dc0e441d9820dfb52bb6df3d200'), ('csrf_token', u'20130702225444##3f1c28cecaf55dc0e441d9820dfb52bb6df3d200'), ('csrf_token', u'20130702225444##3f1c28cecaf55dc0e441d9820dfb52bb6df3d200'), ('location_name', u'b'), ('feed_url', u'bkj'), ('title', u'b'), ('url', u'b'), ('date_crawled', u'b'), ('content_url', u'b'), ('longitude', u'b'), ('latitude', u'b'), ('date_added', u'b'), ('types', u'b')])
>>> keys = ['location_name', 'feed_url', 'title', 'url', 'date_crawled', 'content_url', 'longitude', 'latitude', 'date_added', 'types']
>>> data = [(key, i[key]) for key in i if key in keys]
>>> data
[('location_name', u'b'), ('feed_url', u'bkj'), ('title', u'b'), ('url', u'b'), ('date_crawled', u'b'), ('content_url', u'b'), ('longitude', u'b'), ('latitude', u'b'), ('date_added', u'b'), ('types', u'b')]
>>>
Upvotes: 0