Okay so I'm working on my project and I want the user to be able to visit other users profile so I'll have a link in the HTML page <a href = "/user/5"> User X </a>
and I want them to be redirected to @app.route("/user/"
and having user_id at the end of that path in python using flask which user_id is the 5 but that code doesn't work, is there a way to extract the number after the /user/
so when he clicks on it from an HTML page it would send him to @app.route("/user/5")
and save the 5 in a variable in the python code? so later I can use the 5 in my SQL code to extract the info I need, then when he goes into the site the URL would be something.com/user/5
and the user would be able to to change the 5 to 6 in the url and it would still work taking him to a user number 6 without me having to hard-code a path to every single user?
CodePudding user response:
@app.route("/user/<int:user_id>"
def users(user_id):
print(user_id) # its the url parameter - prints 5 in case of "/user/5"
CodePudding user response:
if you want a variable (user id) in the path, you have to define the decorator and function as below:
@app.route("/user/<user_id>")
def user(user_id):
# your code here, with user_id being the string variable coming from the URL