Dropdown menu from Python list using Flask and HTML

[亡魂溺海] 提交于 2021-02-08 08:52:18

问题


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:


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



来源:https://stackoverflow.com/questions/62864843/dropdown-menu-from-python-list-using-flask-and-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!