Dynamic URLs :
We can also build dynamic URLs by using variables in the URL. To add variables to URLs, use the <variable_name> rule. The function then receives the <variable_name> as a keyword argument.
Benefits of Dynamic URLs:
- It avoids hard coding of the URLs.
- URL building handles the escaping of special characters and Unicode data transparently.
- We can change the URLs dynamically instead of remembering the manually changed hard-coded URLs.
- If your application is placed outside the URL root, for example, in /myapplication instead of /, url_for() properly handles that for you.
- The generated paths are always absolute, avoiding unexpected behavior of relative paths in browsers.
CODE:
`# import flask
from flask import Flask
create an instance(app)
app = Flask(name)
@app.route(‘/’)
define a method
def sample():
return “<b>WELCOME TO MY WEBPAGE</b>”
dynamic Route
@app.route(“/<name>”)
def sample1(name):
return f’Hello {name}'
if name == ‘main’:
app.run()`
OUTPUT:
You can enter any name or word after “/”. The same name or word will be displayed on the webpage, which can be seen in the above images.