Lesson 2: Creating routes in Flask
Welcome to lesson 2 of our Backend Python & Flask series. In this lesson, we will be focusing on creating routes in Flask.
What are routes in Flask?
Routes in Flask are URLs that the application responds to. When a user visits a specific URL, the corresponding route in the Flask application is triggered, and the appropriate response is sent back to the user.
Creating routes
In Flask, routes are defined using the route()
decorator. For example, to create a route that responds to requests to the root URL, you would use the following code:
@app.route('/')
def index():
return 'Hello, World!'
This code creates a route for the root URL (‘/’), and when a user visits this URL, the index()
function is called, and ‘Hello, World!’ is returned as the response.
Passing parameters in routes
Routes in Flask can also accept parameters. These parameters are specified within the route URL, and the corresponding values are passed to the route function as arguments. For example:
@app.route('/user/<name>')
def user(name):
return f'Hello, {name}!'
In this example, the route /user/<name>
will match any URL with the pattern /user/
followed by some text. The value of the text is passed to the user()
function as the name
argument, and the function will return a personalized greeting.
Conclusion
Congratulations! You have now learned how to create routes in Flask. Routes are the backbone of any Flask application, and understanding how to define and work with them is essential for building powerful web applications.
Stay tuned for the next lesson, where we will dive deeper into handling form submissions and working with databases in Flask.
Hello, where will the attachment be shared? I checked the pyansys website as well as the documentation and couldnt find it.
It's not clear where to find attachment which mentioned in this video! There's no direct link in description for this video(