Search code examples
pythonflaskweb-deploymentwerkzeug

What exactly does the {'page': 1} mean here? "BuildError: ('main.user_profile', {'page': 1}, None)"


I'm trying to make a Pagination object for 1 user's posts. Everything's OK when the user is current_user:

@auth.route('/auth/edit_profile', methods=['GET', 'POST'])
def edit_profile():
    ###
    page = request.args.get('page', 1, type=int)
    pagination = current_user.posts.order_by(Post.timestamp.desc()).paginate(
            page, per_page=15, error_out=False)
    posts = pagination.items
    return render_template('auth/edit_profile.html', form=form, 
            user=current_user, posts=posts, pagination=pagination)

However if it's queried within the method like this:

@main.route('/user/<username>')
def user_profile(username):
    target_user = User.query.filter_by(username=username).first()
    if not target_user:
        abort(404)
    page = request.args.get('page', 1, type=int)
    pagination = target_user.posts.order_by(Post.timestamp.desc()).paginate(
            page, per_page=15, error_out=False)
    posts = pagination.items
    return render_template('user_profile.html', user=target_user, posts=posts,
                           pagination=pagination, page=page)

here comes this issue when it's compiled.

werkzeug.routing.BuildError
BuildError: ('main.user_profile', {'page': 1}, None)

I'm aware that this sort of error is related to a route without its specific view function. In this case, does it mean I didn't specify Page-1-content or what?

Please kindly help me.. I'm learning coding from scratch 0.0


Solution

  • Zyber is correct, but here's some more information:

    werkzeug.routing.BuildError
    BuildError: ('main.user_profile', {'page': 1}, None)
    

    is saying that there's no route named user_profile that accepts a named parameter of page. You do have a route named user_profile, but it only accepts a username parameter, as defined in

    @main.route('/user/<username>')
    def user_profile(username):
    

    I can see that you're looking for the pages GET variable in the request:

    page = request.args.get('page', 1, type=int)
    

    Please note that you don't pass the GET parameters to url_for in the template, as that makes Flask render them to part of the URL routing instead of as GET parameters. You don't have a method named user_profile that takes a page variable in the routing.

    Instead, try

    {{ url_for('user_profile', username=[username]) }}?page=[page #]