Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting werkzeug.routing.BuildError

I'm getting a werkzeug.routing.BuildError when I go to my "login.html" page. The problem seems to be the "action=" attribute in the template. Originally it was action={{url_for('login')}}. Although the docs show it done this way it doesn't seem to work. When I change it to action="/login" or action="#" it works correctly. The question is why? I was under the impression that the correct way was the action={{url_for('login')}}?

Before I broke my code up into packages (everything in single py file) it worked correctly.

BTW, most of this code is from Miguel Grindberg's GREAT book "Flask Web Development". The code I'm having problems with is my own that I added going through the book. I'm on WinXP and using the most up to date Flask. Here is my code below:

flasky\app\main\views.py:

from flask import render_template, session, redirect, url_for, current_app, flash
from .. import db
from ..models import User
from ..email import send_email, post_mail
from . import main
from .forms import NameForm, RegForm


@main.route('/login', methods=['GET', 'POST'])
def login():
    form = RegForm()
    if form.validate_on_submit():
        session['name'] = form.username.data
        session['logged_in'] = True
        return redirect(url_for('success'))
    return render_template('login.html', form=form)

flasky\app\templates\login.html:

{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}

{% block title %}Sign Up{% endblock %}


{% block content %}
    {{ super() }}

    <div class="well">
        <h1 align="center">Sign-In</h1>
    </div>

    <div class="container"> 
        <form class="form form-horizontal" action="{{url_for('login')}}" method="post">
            {{form.hidden_tag()}}

            {{wtf.form_field(form.username)}}

            {{wtf.form_field(form.email)}}

            {{wtf.form_field(form.password)}}

            {{wtf.form_field(form.bool)}}

            {{wtf.form_field(form.submit)}}
        </form>
    </div>  
{% endblock %}

<!-- action= {{url_for('login')}} doesn't work. . ."#" and "\login" work-->

flasky\app\main\forms.py:

from flask.ext.wtf import Form
from wtforms import StringField, SubmitField, PasswordField, BooleanField, SubmitField
from wtforms.validators import Required, Email

class RegForm(Form):
    username = StringField('Username', validators=[Required()])
    email = StringField('Email Address', validators=[Email()])
    password = PasswordField('Password', validators=[Required()])
    bool = BooleanField("I Agree To Your Terms of Services", validators=[Required()])
    submit = SubmitField('Submit')
like image 948
Chris Kavanagh Avatar asked Feb 12 '23 12:02

Chris Kavanagh


1 Answers

A BuildError is raised when the url_for() method cannot locate an endpoint that fits the description. In this case, no login endpoint was found to be registered with the Flask app object.

You appear to have registered the login route with a Blueprint named main instead; you need to use the blueprint name in the endpoint name:

{{ url_for('main.login') }}

If the template is only ever used by routes in the main blueprint, you can make it relative too by starting the name with a .:

{{ url_for('.login') }}

See the Building URLs section of the Blueprints documentation.

like image 89
Martijn Pieters Avatar answered Feb 16 '23 03:02

Martijn Pieters