Getting werkzeug.routing.BuildError

I get werkzeug.routing.BuildError when I go to the login.html page. The problem is the "action =" attribute in the template. It was originally action={{url_for('login')}}. Despite the fact that docs shows that it is done in this way , it does not seem to work. When I change it to action="/login"or action="#", it works correctly. The question is why? I got the impression that it was right action={{url_for('login')}}?

Before I broke the code into packages (all in one py file), it worked correctly.

By the way, most of this code belongs to Miguel Grindberg. BIG book "Flask Web Development". The code I'm having problems with is mine, which I added to the book. I am in WinXP and use the latest 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')
+4
source share
1 answer

A BuildErroroccurs when a method url_for()cannot find an endpoint that matches the description. In this case, it was found that the endpoint was loginnot registered in the Flask object app.

You seem to have registered a route loginusing Blueprint named main; you need to use the drawing name in the endpoint name:

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

- main, , .:

{{ url_for('.login') }}

. URL- Blueprints.

+8

Source: https://habr.com/ru/post/1548209/


All Articles