本文介绍了带有Flask Python的werkzeug.routing.BuildError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

**与建议重复的区别,我的错误源于原始代码中缺少以下代码行 session ['message'] = request.form ['message'] 在建议重复的地方缺少 render_template 组件

**difference to the suggested repeat, my error stemmed from the following line being missing in the original code session['message']=request.form['message'] wherease in the suggested duplicate was missing the render_template component`

我试图用Flask创建用户会话,我不在乎认证。我只想要一个页面,他们输入他们的名字,然后他们被重定向到主页面。我试图按照,但我得到一个 werkzeug.routing.BuildError 。总结我的python应用程序是:

I am trying to create user sessions with Flask, I don't care about authentication. I just want a page where they enter their name, and then they are redirected to the main page. I tried to follow the example in this link here but I get a werkzeug.routing.BuildError. To summarise my python app is:

from flask import Flask, render_template
from flask import request, session, url_for,abort,redirect

app = Flask(__name__)
app.config['SECRET_KEY'] = 'F34TF$($e34D';

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

@app.route('/signup', methods=['POST'])
def signup():
    session['username'] = request.form['username']
    session['message']=request.form['message']
    return redirect(url_for('message'))

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

if __name__ == '__main__':
    app.run(debug=True)

index.html 是:

and index.html is:

{% extends "layout.html" %}
{% block content %}
    <h1>Say something</h1>
    <form method="post" action="{{ url_for('signup') }}">
        <p><label>Username:</label> <input type="text" name="username"    required></p>
        <p><button type="submit">Send</button></p>
    </form>
{% endblock %}

layout.html 是:

layout.html is:

<!doctype html>
<html lang="en">
    <head>
        <title>Say somthing</title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
       <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
    </head>
    <body>
        {% block content %}{% endblock %}
    </body>
</html>


推荐答案

你得到那个错误是因为你没有一个名为 message 的路由,然后你重定向到它。

You are getting that error because you don't have a route called message and yet you are redirecting to it.

@app.route('/signup', methods=['POST'])
def signup():
session['username'] = request.form['username']
# Create a message route first
return redirect(url_for('message'))

示例路由调用message

Here's a sample route called message

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

这篇关于带有Flask Python的werkzeug.routing.BuildError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 07:01