简体   繁体   中英

Dropdown menu from Python list using Flask and HTML

I am creating a dropdown menu in HTML using info from a python script. I have inspired myself from this StackOverflow question: How to create dropdown menu from python list using Flask and HTML

However, when I try to retrieve back the selected answer, I get (also tried with 'colour' and gave me the same problem):

select = request.form.get(colours)
NameError: name 'colours' is not defined

This is my __init__.py which is the main of my Flask application, and I have this route inside which is suppose to retrieve the element that was selected by the dropdown and returns the selected value (simply displayed on the web browser):

@app.route("/upload", methods=["POST", "GET"])
def upload():
if "email" in session:
    if request.method == "POST":
        select = request.form.get(colours)
        return str(select)
    else:
        return render_template("upload.html", colours=dirs)
else:
    return render_template("index.html")

This is my upload.html which contains the HTML code for the dropdown:

 <form  action="{{ url_for('upload') }}" method="post">

            <div>
                <select class="browser-default custom-select" name= colours method="POST" action="/" >
                    {% for colour in colours %}
                    <option value= "{{colour}}" SELECTED>{{colour}}</option>"
                    {% endfor %}
                </select>
            </div>

    <input type="image" src="https://thisisapicture.png" style="height: 45px">
</form>

How can I retrieve the user's choice for the dropdown as neither request.form.get(colours) and request.form.get(colour) were 'not defined'?

<form action="{{ url_for('upload') }}" method="post">

Your current form tag's action attribute isn't defined so it's not sending data anywhere when you submit it.

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