[Flask] "could not build URL for endpoint"?

Anthony Ford ford.anthonyj at gmail.com
Wed May 11 09:32:45 EDT 2016


Your running into an uissue in that url_for doesn't take the path ( the
"/foo/bar" in "example.com/foo/bar") but the function which provides the
view you want.

If you define a route (say "/index"), and the function that handles that
route is foobar(), then you would use url_for('foobar').

So for:

@app.route('/index')
def foobar():
    return render_template('foobar.html')


your template would have the following link:

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


Hope that helps!

Also, be careful with your quotes. While Jinja should pick up the quotes
within {{}} and handle them well, even with the outer quotes, it's good to
be in the habit of not mixing quotes. Use one type (i.e. double) for your
outer, and the other (single) for your inner. Since Python doesn't
differentiate between the types, it's safe to swap them as you desire.



Anthony Ford,
KF5IBN,
ford.anthonyj at gmail.com

On Wed, May 11, 2016 at 8:22 AM, Alex Hall <ahall at autodist.com> wrote:

> Hello list,
> I have no idea what I changed--I've been messing with so many config files
> and apps lately--but suddenly I'm getting that error when I load my app. My
> templates use the 'url_for' function, as I'm told they should, but that
> seems to be failing for some reason. The only related change I can think of
> is that I recently added a couple more decorators to a view function, in an
> effort to always direct to a certain view when visitors go to the main
> site. That is
> myapp.mysite.com/search
> is redirected to by
> myapp.mysite.com
> or
> myapp.mysite.com/index
>
> The error is "could not build URL for endpoint '/index'. Did you mean
> '/search' instead?
>
> In my base template, I define a navigation bar. In the exception, the
> first url in that bar is causing the problem:
> a href="{{url_for("/index")}}">Home</a
>
> Might the problem be that I'm defining a link (/index) that will render as
> this view, because that's a decorator before the /search function? That
> doesn't make much sense to me, but I can't think what else it could be.
> Related: is there a better way of directing to a certain view than putting
> a bunch of app.route decorators in front of a single function? Is the
> recommended way to handle this in the web server configuration instead?
>
>
> --
> Alex Hall
> Automatic Distributors, IT department
> ahall at autodist.com
>
> _______________________________________________
> Flask mailing list
> Flask at python.org
> https://mail.python.org/mailman/listinfo/flask
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20160511/733bde27/attachment.html>


More information about the Flask mailing list