1

So, I am very new to flask and front end programming..

I have a form: like this

<form action="{{ url_for('search') }}" method="post">
    Showing values for: <input type="text" name="name" value={{ name }} disabled><br><br>
    <select name="movie" width="300px">
        {% for movie in movie_list %}
        <option value="{{ movie }}" SELECTED>{{ movie }}</option>
        {% endfor %}
    </select>
    <input type="submit" value="Recommend!!">
</form>

And this is how I am capturing in flask

@app.route('/web/search', methods=['post','get'])
def search():
    print request.method, request.form

So, you can see, that in html form.. I have two fields..

name (text box) which already have a default value set

and option_list

And, my intention is, when the user presses "submit", both these fields are received in backend

But the print statement shows..

POST ImmutableMultiDict([('movie', u'foobar')])

But I dont see the name field?

How do I fix this?

vabada
  • 1,738
  • 4
  • 29
  • 37
frazman
  • 32,081
  • 75
  • 184
  • 269

2 Answers2

4

Probably because you're missing quotes around the value for the first input.

Also disabled inputs won't be passed, values of disabled inputs will not be submited?

Community
  • 1
  • 1
postelrich
  • 3,274
  • 5
  • 38
  • 65
2

You are allowing both GET and POST requests, but you don't differentiate between them in your code. If you want to grab those values when the user submits them, then you need to do something when that form POSTs. Perhaps something like:

@app.route('/web/search/', methods=['POST','GET'])
def search():
    if request.method == 'POST':
        print request.method, request.form
        # do some stuff
        return redirect(url_for('search_results', results=results))
    else:
        return render_template('my_template.html')

Then, in the if statement, you can do things with the variables. In my imaginary case here, I'm sending the user to another web page where you can show the results of the search. I just made up a template name for render_template(), so you can substitute in whatever your template is actually called.

coralvanda
  • 6,431
  • 2
  • 15
  • 25