簡體   English   中英

燒瓶錯誤:werkzeug.routing.BuildError

[英]Flask error: werkzeug.routing.BuildError

我修改了flaskr示例應用程序的登錄,第一行出錯。 但是 www.html 在模板目錄中。

return redirect(url_for('www'))
#return redirect(url_for('show_entries'))

顯示錯誤:

werkzeug.routing.BuildError

BuildError: ('www', {}, None) 

return redirect(url_for('www'))如果你在其他地方有這樣的函數,它會起作用:

@app.route('/welcome')
def www():
    return render_template('www.html')

url_for查找一個函數,您將要調用的函數的名稱傳遞給它。 可以這樣想:

@app.route('/login')
def sign_in():
    for thing in login_routine:
        do_stuff(thing)
    return render_template('sign_in.html')

@app.route('/new-member')
def welcome_page():
    flash('welcome to our new members')
    flash('no cussing, no biting, nothing stronger than gin before breakfast')
    return redirect(url_for('sign_in')) # not 'login', not 'sign_in.html'

你也可以做return redirect('/some-url') ,如果這更容易記住。 考慮到您的第一行,您想要的也可能只是return render_template('www.html')

而且,不是從下面的 shuaiyuancn 的評論中,如果您使用藍圖,則url_for應該作為url_for('blueprint_name.func_name')調用注意您不是傳遞對象,而是字符串。 請參閱此處的文檔

假設def www():已經定義(正如 unmounted 的很棒的答案所建議的那樣),如果您使用的是尚未注冊的藍圖,也可能會拋出此錯誤

確保在第一次實例化app時注冊這些。 對我來說,它是這樣完成的:

from project.app.views.my_blueprint import my_blueprint
app = Flask(__name__, template_folder='{}/templates'.format(app_path), static_folder='{}/static'.format(app_path))
app.register_blueprint(my_blueprint)

my_blueprint.py

from flask import render_template, Blueprint
from flask_cors import CORS

my_blueprint = Blueprint('my_blueprint', __name__, url_prefix='/my-page')
CORS(my_blueprint)


@metric_retriever.route('/')
def index():
    return render_template('index.html', page_title='My Page!')

我遇到了這個錯誤

BuildError: ('project_admin', {}, None)

當我接到一個電話時

return redirect(url_for('project_admin'))

我試圖在我的藍圖中引用project_admin函數。 為了解決這個錯誤,我在函數名前加了一個點,像這樣:

return redirect(url_for('.project_admin'))

瞧,我的問題解決了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM