Home > Enterprise >  How can I use url_for in jinja?
How can I use url_for in jinja?

Time:08-19

I have a issue during using url_for in jinja template. I use like :

href="{{ url_for('user',user_id=current_user.id)}}"

when I write href="user/{{current_user.id}}" it works perfectly ::

and my view function is :

@app.route('/user/<user_id>')
def get_user(user_id):
    user = Users.query.filter_by(id = user_id).first()
    return render_template("profile.html", user = user)

but my jinja cant see this url and has an errors

werkzeug.routing.exceptions.BuildError: Could not build url for endpoint 'user' with values ['user_id']. Did you mean 'get_user' instead

CodePudding user response:

As the error message suggested, you have to provide the name of the view function to url_for():

href="{{ url_for('get_user', user_id=current_user.id)}}"
  • Related