1

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'?

1 Answer 1

1
<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.

Sign up to request clarification or add additional context in comments.

5 Comments

Sorry for the mistake but I still have the same problem even with this
You're also missing a submit button within the form. How are you submitting it?
The submit button is suppose to be the image. I inspired myself from this : stackoverflow.com/questions/14199788/…
Try select = request.form.get("colours") to access the form data. Note the "colours" in quotes because you're using it as a key.
Thank you it was the problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.