在我的Flask应用程序中,我制作了一堆a hrefs
,它将把用户重定向到另一个包含输入表单的页面。此输入表单将通过this.innerHTML
预先填写。它在除firefox之外的所有浏览器上都可以正常运行,在该浏览器中我得到ValueError: View function did not return a response
。
{% for x in text %}
<p>{{ x["address"] }} : <strong><a href="">{{ x["car_park_no"] }}</a></strong> </p>
{% endfor %}
这对我来说尤其令人困惑,因为我已经在Google和SO上进行了检查,并尝试了与Firefox相关的所有可能的修复方法……(window.location)
vs (window.location.href)
vs (window.location.assign)
与make function return false
我了解SO上存在有关仅在函数不返回响应时如何发生此错误的问题-但是,这里的函数已经返回了响应。而且在Chrome / Internet Explorer / Safari / Edge上不会发生此错误,仅在Firefox上会发生。
<script>
// add query strings
window.onload = function() {
var elements = document.getElementsByTagName('a');
for(var i = 0, len = elements.length; i < len; i++) {
elements[i].onclick = function () {
event.preventDefault();
window.location = "/lotfinder?address=" + this.innerHTML;
}, false;
}
}
</script>
任何帮助将不胜感激,如果需要更多代码段,请发表评论。
编辑:/ lotfinder端点处的代码
<h1>carpark.py</h1>
<form action = "/carpark" method = "POST">
<p>Carpark No: <input type = "text" id="carpark-input" name = "Carpark" required></p>
<p><input type = "submit" value = "submit"></p>
</form>
</body>
<script>
const carparkInput = document.getElementById("carpark-input");
// use new URLSearchParams API instead of ugly regex querystring parsing
window.onload = function() {
var urlParams = new URLSearchParams(window.location.search);
carparkInput.value = urlParams.get('address');
}
</script>
上面是HTML,下面是处理程序
@app.route('/lotfinder',methods = ['POST', 'GET'])
def car1():
if request.method == 'GET':
return render_template("car1.html")
答案 0 :(得分:0)
我认为您正在为所有点击事件处理程序分配false
。试试这个:
// add query strings
window.onload = function() {
var elements = document.getElementsByTagName('a');
for(var i = 0, len = elements.length; i < len; i++) {
elements[i].onclick = function () {
event.preventDefault();
window.location = "/lotfinder?address=" + this.innerHTML;
};
}
}
当您的函数之一未返回值时,Flask将返回ValueError: View function did not return a response
错误。参见Flask Value error view function did not return a response
您可以尝试编辑端点函数,如下所示:
@app.route('/lotfinder',methods = ['POST', 'GET'])
def car1():
if request.method == 'GET':
return render_template("car1.html")
return 'OK'
答案 1 :(得分:0)
只需将事件作为参数传递给匿名函数
elements[i].onclick = function ( event ) {
谢谢