简体   繁体   中英

Python Flask Form - html Selectbox returning 'None' as the selected item

I am creating a website server program with Flask and Python, I am relatively new to Flask. I have created a html form with a Select tag (drop down menu, single select). You can see the code below.

selectmenu.html

<!doctype html>
<html>
<body>
    <form id="form" method="POST" class="selectform">
        <select method="GET" size=5>
            {% for x in options %}
            <option value="{{x}}" SELECTED>{{x}}</option>
            {% endfor %}
        </select>
        <input type="submit" name="go"></input>
    </form>
</body>
</html>

The Python code for Flask: app.py

@app.route('/selectmenu',methods=(['GET','POST']))
def selmenu():
    optionfiles = os.listdir('options')
    form = selectform()
    if request.method == 'POST':
        selectedoption = form.selectedoption.data
        print(selectedoption)
        return redirect('/')
    return render_template('selectmenu.html',options=optionfiles,form=form)

And finally the Python WTForms classes code forms.py :

class selectform(FlaskForm):
    optionfiles = os.listdir('userdata')
    print(optionfiles)
    selectedoption = SelectField('selectdropdown', choices=optionfiles,validators=[DataRequired()])
    submitbutton = SubmitField('submit',validators=[DataRequired()])

But when the program called form.selectoption.data it returns None every time.

I have tried many tutorials, and Stackoverflow answers, but still can't get data from the form that isn't None .

NOTE: As you can tell I have uploaded snippets only of the code, as the actual files are all a lot larger, If you need any other bits of the code. e:g, the imports, then please ask, but I am pretty sure the error isn't the imports. for example! Otherwise I would receive an ImportError.

Thank you in advance for your help!

It seems that you are using Flask-WTF (FlaskForm) but the way you write the html part is not compliant with it. To confirm you can replace selectmenu.html by:

<!doctype html>
<html>
<body>
    <form method="POST">
        {{ form.selectedoption() }}
        {{ form.csrf_token }}
        {{ form.submitbutton(class_='btn btn-primary') }}
    </form>
</body>
</html>

If it is the case you might have a look at https://flask-wtf.readthedocs.io/en/stable/quickstart.html and https://flask.palletsprojects.com/en/1.1.x/patterns/wtforms/#forms-in-templates (for the easy template).

(And return render_template('selectmenu.html',options=optionfiles,form=form) can be replaced by return render_template('selectmenu.html',form=form) )

After finding this Stackoverflow post I found the answer.

I needed to use give the <select> tag a name, like this: name="optionsbox" in the html file. Then do request.form.get("optionsbox") in the app.py file, instead of form.selectedoption.data . No changes needed in the forms.py file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM