'How to request header in Flask? Or how to get a select option tag's value? [duplicate]
I want to get select value in HTML and use in python Flask. Because I could not find any way to get select tag option value via Flask, I get the value with JavaScript and then span it in a header. Now, I want to request the header but I could not. I get Key Error. Here is my HTML code.
<label for="filtre_input">Neye göre karşılaştırmak istersiniz?</label>
<select name="filtre_input" id="filt" onchange="filtreinputla();">
<option selected="selected">Kategori</option>
<option value="il">İl</option>
<option value="tur">Tür</option>
<option value="renk">Renk</option>
<option value="duygu">Duygu</option>
</select>
<script type="text/javascript">
function filtreinputla() {
var filtreselect = document.getElementById("filt").value;
console.log(filtreselect);
document.getElementById("filtreselectid").innerHTML = filtreselect;
}
</script>
<h3 id = "filtreselect_p" type = "hidden" name="filtreselect_p"><span id="filtreselectid"></span></h3>
Here is my app.py code.
@app.route("/nd", methods=['POST', 'GET'])
def func():
flash("")
filter = request.headers["filtreselect_p"]
if filter == None:
flash("Please, select something.")
return render_template("index.html")
else:
result = my_function(filter)
flash(result)
return render_template("index.html")
I want to request the header or find a new approach to get select option tag value. Please, help if you can :)
Solution 1:[1]
You should use form tag.
You can use GET or POST method.
<form method="get" action="/ng"><--or method="post"-->
<label for="filtre_input">Neye göre kar??la?t?rmak istersiniz?</label>
<select name="filtre_input" id="filt" onchange="filtreinputla();">
<option selected="selected">Kategori</option>
<option value="il">?l</option>
<option value="tur">Tür</option>
<option value="renk">Renk</option>
<option value="duygu">Duygu</option>
</select>
</form>
If you use GET method your flask code is
request.args.get('filtre_input')
If you use POST method you can use
request.form.get('filtre_input')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user17517503 |
