We can set route dynamically instead of fixed route. For example In the pre-determined route links generate like this example.com/users/profile In the dynamically gendered route will be like this example.com/users/custom_profile_name
We will use
@app.route('/<username>')
The code will be like this
@app.route('/<username>')
def show_profile(username):
return username
We can use anything in the <username> here So if we load page like example.com/anything_here It will show the route. This is the complete example. Copy and try and customize the code by yourself.
from flask import Flask, url_for
app = Flask(__name__)
@app.route('/<username>')
def show_profile(username):
return username
@app.route('/')
def index():
example_link = url_for('show_profile', username='example')
return f"You are on index route. Go to <a href='{example_link}'>/example</a> for an example."
if __name__ == '__main__':
app.run(debug=True)