0

In my Flask App, I'm making a bunch of a hrefs that will redirect the user to another page that contains an input form. This input-form will be pre-filled via this.innerHTML It works perfectly on all browsers except for firefox, where I get the 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 %}

This is particularly confusing for me as I've checked everywhere on Google and SO and tried all the possible fixes that could be Firefox related... (window.location) vs (window.location.href) vs (window.location.assign) vs make function return false

I understand that there are questions on SO regarding how this error only occurs when functions do not return a response - However my function here already returns a response. And this error does not occur on Chrome/Internet Explorer/Safari/Edge, only on 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>

Any help would be appreciated, do leave a comment if you require more code snippets.

edit: Code at /lotfinder endpoint

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

Above is HTML, below is the Handler

@app.route('/lotfinder',methods = ['POST', 'GET'])
def car1():
    if request.method == 'GET':
        return render_template("car1.html")
sgeza
  • 341
  • 1
  • 5
  • 16

2 Answers2

0

I think you are assigning false to all your click event handlers. Try this:

 // 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;
     };
   }
 }

The ValueError: View function did not return a response error is returned by Flask when one of your functions does not return a value. See Flask Value error view function did not return a response

You can try editing your endpoint function like so:

@app.route('/lotfinder',methods = ['POST', 'GET'])
def car1():
    if request.method == 'GET':
        return render_template("car1.html")
    return 'OK'
tomcek112
  • 1,516
  • 10
  • 16
  • yeah i thought that too, but the error of `ValueError: View function did not return a response` still persists, weird thats its Firefox-only – sgeza Oct 08 '18 at 08:19
  • Done @tomcek112 – sgeza Oct 08 '18 at 08:25
  • edited according to your answer, even removed the `if request.method ==` handler but the err 500 persists, interestingly enough, on Firefox, the endpoint is a `GET /address` instead of a `GET /lotfinder?querystringhere` on other browsers – sgeza Oct 08 '18 at 09:57
0

Just pass event into anonymous function as parameter

elements[i].onclick = function ( event ) {

Thanks

Vinesh Goyal
  • 607
  • 5
  • 8
  • hi @vinesh what does passing event into the function do? tried but 500 persists on firefox – sgeza Oct 08 '18 at 09:59