Forums

Flask. werkzeug.routing.BuildError

While building a flask application a got a problem with using {{ url_for() }} in my template.

I have this in my app.py:

@app.route("/revisal")
def revisal():
  return render_template("questions.html")

And this in one of my templates:

<a href="{{ url_for('revisal') }}">...</a>

Everything works as intended on my local machine but I get a server error when I upload it to pythonanywhere. Here is error log:

2015-11-10 16:38:07,126 :Exception on /results [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.4/dist-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/ignat/studyct/app.py", line 51, in process_form
    result = score)
  File "/usr/local/lib/python3.4/dist-packages/flask/templating.py", line 128, in render_template
    context, ctx.app)
  File "/usr/local/lib/python3.4/dist-packages/flask/templating.py", line 110, in _render
    rv = template.render(context)
  File "/usr/local/lib/python3.4/dist-packages/jinja2/environment.py", line 969, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/local/lib/python3.4/dist-packages/jinja2/environment.py", line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.4/dist-packages/jinja2/_compat.py", line 36, in reraise
    raise value.with_traceback(tb)
  File "/home/ignat/studyct/templates/results.html", line 1, in <module>
    {% extends "base.html" %}
  File "/home/ignat/studyct/templates/base.html", line 17, in <module>
    {% block content %}
  File "/home/ignat/studyct/templates/results.html", line 27, in <module>
    <a href="{{ url_for('revisal') }}"><h2>Работа над ошибками</h2></a>
  File "/usr/local/lib/python3.4/dist-packages/flask/helpers.py", line 312, in url_for
    return appctx.app.handle_url_build_error(error, endpoint, values)
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1641, in handle_url_build_error
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.4/dist-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/local/lib/python3.4/dist-packages/flask/helpers.py", line 305, in url_for
    force_external=external)
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/routing.py", line 1616, in build
    raise BuildError(endpoint, values, method)
werkzeug.routing.BuildError: ('revisal', {}, None)

Pls help.

Looking at the stacktrace and your code, I see three different things called app. There's your app.py file, the app variable that you're adding the routes to and the flask.app module. I think it's highly likely that flask is looking at the wrong one when it's trying to build routes. The reason it may work on your local machine is that the PYTHON_PATH would be different and so the aliasing wouldn't be noticeable.

Thanks, glenn!

I renamed everything and now it works. I dont understand why and how, but it fixed the problem.

Hi, I got a similar error, but I think there is a different problem. I have a form on my web, and I use POST method to transfer data from webpage to flask. I use the following code to show an answer to the user based on the input data:

return redirect(url_for("example", answer = text))

on JavaScript I use:

    const urlParams = new URLSearchParams(window.location.search);
    const myParam = urlParams.get('answer');

    document.getElementById('answer').innerHTML = myParam

everything works on local system, but I get the following error in pythonanywhere:

2021-05-25 16:33:30,743: Exception on / [POST]
Traceback (most recent call last):
  File "/home/pariasrz/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/pariasrz/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/pariasrz/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/pariasrz/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/pariasrz/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/pariasrz/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/pariasrz/health_tracker/main.py", line 37, in login
    return redirect(url_for("example", answer = text))
  File "/home/pariasrz/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/helpers.py", line 370, in url_for
    return appctx.app.handle_url_build_error(error, endpoint, values)
  File "/home/pariasrz/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/app.py", line 2216, in handle_url_build_error
    reraise(exc_type, exc_value, tb)
  File "/home/pariasrz/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/pariasrz/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/helpers.py", line 357, in url_for
    rv = url_adapter.build(
  File "/home/pariasrz/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/werkzeug/routing.py", line 2179, in build
    raise BuildError(endpoint, values, method, self)
werkzeug.routing.BuildError: Could not build url for endpoint 'example' with values ['answer']. Did you mean 'result' instead?

result.html is another page of my web. I don't understand the problem in the last line.

I will be grateful if you can help.

What is the definition of the view that you're referring to as "example" in your call to url_for?

example.html is a page which should be loaded when the form is submitted. The answer should be shown on this page. I have the following code for it:

@app.route("/example.html")
def example():
    return render_template("example.html")

You've named your endpoint example.html, but then you're trying to use just example to refer to it. Change one of them so that they match.

Thank you. I changed this:

return redirect(url_for("example.html", answer = text))

but still not working and I get the same error :(

Ah! Sorry. I was wrong. The argument to url_for needs to be the name of the function for the view. That then suggests to me that you are not actually defining that view. What happens when you visit /example.html on your web app?

oh! it actually shows the html page but without the answer it gets from the flask.

Take a look at https://flask.palletsprojects.com/en/1.1.x/api/#flask.url_for

Thank you! it helped a lot. it's working now!

Excellent -- thanks for letting us know!