Can't identify what's causing Flask Werkzeug BuildError

时间:2019-01-07 13:14:03

标签: python html flask werkzeug build-error

I use URL parameters on my site as identifiers for user data. If a user clicks one specific link on the site, these parameters will be 'none', and I try to use the following function to then send them back to the same page with the correct parameters:

<a href="{{ url_for('site', param1='none', param2='none', param3='none', param4='none') }}">Site</a>

@app.route('/my-site/<param1>/<param2>/<param3>/<param4>', methods=['GET', 'POST'])
def site():

    if param1 == 'none':

       linking_data = Posts.query.filter_by(user=current_user.email).first()

       return render_template('site', param1=linking_data.param1, 
                                      param2=linking_data.param2, 
                                      param3=linking_data.param3, 
                                      param4=linking_data.param4)

If the params are hard-coded, this approach works well. If I attempt to define the params as I have above however, it results in the following error:

werkzeug.routing.BuildError: Could not build url for endpoint 'site' with values ['param3', 'param4'].     
Did you forget to specify values ['param1', 'param2']?

Why does it think I haven't specified the first two parameters?

1 个答案:

答案 0 :(得分:0)

您需要将url参数添加到处理程序中:

@app.route('/my-site/<param1>/<param2>/<param3>/<param4>', methods=['GET', 'POST'])
def site(param1=None, param2=None, param3=None, param4=None):
    ....

提示:None值输出为字符串None,而不是none。如果您更新代码以处理不区分大小写的“ none”字符串值会更好。