Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing Web Form Values
#1
Hey guys,

I'm sorta new to web development and Python, and I have a question around passing values from the web form.

So I have a web page called Form.html, and I want to capture the values in my python script, so then I can pass those values into another method to compose a custom message.

A sample code from python looks like the following:
@app.route('/form', methods=['GET', 'POST'])
def form():

    if request.method == "POST":
       txtvalue1 = request.form['txtValue1']
.
.
.
       txtvalue20 = request.form['txtValue20']
       
So I'm able to capture the values like above no problem, but my question to you is, if I want to pass those values into another method do I have to do it as such?
custommessage(txtvalue1, txtvalue2, ..., txtvalue20)
I just feel like there are so many values to pass, but is that best practice or is there a better way to do it?

Moderator sparkz_alot:
Please use code tags when posting code.
Reply
#2
return value1, value2
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
It work a little different,you return but also redirect to an other route.
@app.route('/form', methods=['GET', 'POST'])
def form(): 
   if request.method == "POST":
        txtvalue1 = request.form['txtValue1']
   return redirect(url_for('foo', name=txtvalue1))
Now can use it in foo route:
@app.route('/foo/<name>')
def foo(name):
    return "hello Boss {}".format(name)
If want to send it back out to html,then use jinja(build into Flask).
@app.route('/foo/<name>')
def foo(name):
    return render_template('bar.html', name=name)
<div class="bar-data">  
  <h1>{{ name }}</h1>
</div>
Reply
#4
Thanks guys for the comments!

My question is no so much on how to pass the values, but it's more on the lines of the amount of values I need to pass.  I have over 20+ text boxes in my web form for the information I need to record.  I was looking for a simpler way to pass the values, but there probably is no easier way around, I would have to detail out every single values into variables using the request.form command.

txtvalue1 = request.form['txtValue1']
txtvalue2 = request.form['txtValue2']
txtvalue3 = request.form['txtValue3']
txtvalue4 = request.form['txtValue4']
txtvalue5 = request.form['txtValue5']
txtvalue6 = request.form['txtValue6']
txtvalue7 = request.form['txtValue7']
.
.
.
txtvalue20 = request.form['txtValue20']
Reply
#5
request.form returns a MultiDict object.
So it already return data structure,you can build you own with list or dict if it dos not what you want.
Quote:A MultiDict is a dictionary subclass customized to deal with multiple values for the same key,
which is for example used by the parsing functions in the wrappers.
This is necessary because some HTML form elements pass multiple values for the same key.
You can be loop over MultiDict like this:
f = request.form
for key in f.keys():
    for value in f.getlist(key):
        print('{} --> {}'.format(key, value))
Reply
#6
That is interesting.  Thank you so much, I'll check it out!
Reply
#7
So I managed to bring in all the form values using the way you suggested.  What I did, since I needed them in a specific order, I passed all the values into a List, since the dictionary is not able to sort.  So after that I want to turn all those values into json format, but I'm running into a formatting issue.


my_list = []

my_dictionary = request.form


        for key in my_dictionary.keys():


            for value in my_dictionary.getlist(key):


                message = ("title:{}, values:{}".format(key, value))


                my_list.append(message)


        

        my_list.sort()
        return jsonify(my_list)
When I execute the code above, it does work and I get the following result:

  "title:txtAppName, values:windows",
  "title:txtAppNumberOfDatabases, values:2",
  "title:txtAppPurpose, values:insert data",
  "title:txtAppRecoverySLA, values:N/A",
  "title:txtAppSupportPage, values:N/A"

What I'm looking for is:

  "title":"txtAppName", "values":"windows",
  "title":"txtAppNumberOfDatabases", "values":"2",
  "title":"txtAppPurpose", "values":"insert data",
  "title":"txtAppRecoverySLA", "values":"N/A",
  "title":"txtAppSupportPage", "values":"N/A"

I can't seem to get the extra double " around all the values, I keep getting a \" instead.
Reply
#8
Instead of appending a string, append an object. Then jsonify will encode it as a json object, instead of as a string.

message = {
    title: key,
    values: value
}
mylist.append(message)
Reply
#9
Thanks!  I finally got it working, but I ended up having to use json.dumps instead.

        my_list = []

        my_dictionary = request.form

        for key in my_dictionary.keys():
            for value in my_dictionary.getlist(key):
                
                message = {"title":key, "values":value}
                my_list.append(message)
        
        my_list.sort()
        return json.dumps(my_list, sort_keys=True)
I also had to do sort_keys=True because it kept putting the "values" column before "title".

Thanks again guys for the help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Jinja sort values from request.form SpongeB0B 2 2,185 Jul-26-2020, 07:41 AM
Last Post: SpongeB0B
  Pre-populating WTForms form values for edit danfoster 0 2,349 Feb-25-2020, 01:37 PM
Last Post: danfoster

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020