lingthio / Flask-User

Customizable User Authorization & User Management: Register, Confirm, Login, Change username/password, Forgot password and more.
http://flask-user.readthedocs.io/
MIT License
1.06k stars 294 forks source link

werkzeug.routing.BuildError: Could not build url for endpoint #246

Closed zacqed closed 2 years ago

zacqed commented 5 years ago

Flask_user is breaking the URL links defined while include a header.html file outside of its own library. The header file works perfectly rendering all links across the site except for templates under flask_user. The example is as follows:

I have modified the register to include an header template as follows:

{% extends "flask_user/_public_base.html" %}
{% block content %}
{% from "flask_user/_macros.html" import render_field, render_submit_field %}

<!-- Breadcrumb-->
<div class="breadcrumb-holder">
    <div class="container-fluid">
        <ul class="breadcrumb">
            <li class="breadcrumb-item"><a href="home"> <b> Home </b></a></li>
            <li class="breadcrumb-item active">Admin / Registration </li>
        </ul>
    </div>
</div>

The sidebar within the header file goes something like this:

<div class="main-menu">
                <h5 class="sidenav-heading"></h5>
                <ul id="side-main-menu" class="side-menu list-unstyled">
                    <li><a href="**{{ url_for('.home') }}**"> <i class="fa fa-pie-chart"></i>Home</a></li>
                    <li>
                        <a href="#exampledropdownDropdown" aria-expanded="false" data-toggle="collapse"> <i class="fa fa-adn"></i>Admin </a>
                        <ul id="exampledropdownDropdown" class="collapse list-unstyled ">
                            <li><a href="{{ url_for('user.register') }}">User Registration</a></li>
                            <li><a href="**{{ url_for('.view') }}**">View User</a></li>
                        </ul>
                    </li>
                    <li>

The .home and .view give error in logs leading to 500 Internal server error: The log says following

File "C:\Users\HarshitGupta\Desktop\NEw\KoreD\KoreD\templates\flask_user\_common_base.html", line 1, in top-level template code
    {% extends "flask_user/header.html" %}
  File "C:\Users\HarshitGupta\Desktop\NEw\KoreD\KoreD\templates\flask_user\header.html", line 60, in top-level template code
    <li><a href="{{ url_for('.home') }}"> <i class="fa fa-pie-chart"></i>Home</a></li>
  File "C:\Users\HarshitGupta\Anaconda3\lib\site-packages\flask\helpers.py", line 356, in url_for
    return appctx.app.handle_url_build_error(error, endpoint, values)
  File "C:\Users\HarshitGupta\Anaconda3\lib\site-packages\flask\app.py", line 2061, in handle_url_build_error
    reraise(exc_type, exc_value, tb)
  File "C:\Users\HarshitGupta\Anaconda3\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "C:\Users\HarshitGupta\Anaconda3\lib\site-packages\flask\helpers.py", line 345, in url_for
    force_external=external)
  File "C:\Users\HarshitGupta\Anaconda3\lib\site-packages\werkzeug\routing.py", line 1776, in build
    raise BuildError(endpoint, values, method, self)
**werkzeug.routing.BuildError: Could not build url for endpoint 'user.home'. Did you mean 'user.login' instead?**

Any fix or help is appreciated

ghost commented 5 years ago

@Harshitg Question about this line: <a href="**{{ url_for('.home') }}**">

Is that supposed to be your website home (e.g. not a flask-user extension)? I'd expect that to be true, and in this case you need to change ',home' and '.view' to 'home' and 'view' respectively (no dots).

When you put a . in front of the string you pass to url_for, that tells it to look for an endpoint in the current blueprint(which is user blueprint and not your app). By leaving the . off, you tell it to look for a endpoint in the app.

supportcodinghub commented 4 years ago

ghost , thanks