Flask App Routing - Collegeintegral



Q.) What is app routing in flask?

Ans. App routing means assigning different URLs to specific functions to perform different tasks.
This means using different URLs and providing them with different functions to get a different value, each time we enter a different URL.

To assign a URL to a particular task we need to use the “@app.route” decorator. With the help of this command, we can use different URLs to get different outputs. With the help of flask, we can also build dynamic URLs by taking the help of variables in the URL.
For adding variable we use<add_variable_here>.

Now after using this command for creating a dynamic URL you have to pass your variable name as an argument in the search bar.
In addition, this flask also provides us a feature to change the data type of the variable we created and for that “<converter:add_variable_here>” is used.
Now let us take our attention to the type of results we can get from URLs.
There are three basic ways in which an URL is gonna give the response,

1.) Page template: It means giving us a page as an output that is most probably written in HTML, CSS, and JavaScript.

2.) Providing a response: These are the most basic types of responses in which you can get a word, sentence, or a mathematical value as an output.

3.) Redirecting the user to some other web page: In this, we use a string which is basically a path for the user to some other web page.

Let's take a deeper dive,

1.) Page Template:
As we all know that flask serves its web pages with a built-in template engine that is known as jinja2.
Now to take help from this template we import a built-in flask function that is known as “render-template”.
This function is very much popular with flask users as with the help of this we can use HTML, CSS, and JavaScript in flask and make a basic outline for our web page.
So, when we use “render-template” it basically is telling the flask to serve an HTML page that has been generated with the help of jinja2.

2.) Providing a Response:
If you are creating an endpoint just to respond to the user with some particular value, in that case, we use the “make_response” command in the place of “render_template”.
This is because with this we can just make a one-word or sentence response or just respond with the help of a number.

3.) Redirecting the user:
The last option for a URL to respond to a user is to redirect the user to some other view. In this, we use “redirect” which accepts a string and this string is basically a path to redirect the user.

Now we are gonna talk about a global object that can be very useful in flask as it provides us with many attachments that can be used for different purposes. this object is known as “request()”.

Now, the request provides us with many attachments but we are gonna talk about a just few of them.
request. method (with the help of this method we can access our routes, with the help of keywords like get), request.form(now this command is useful when you are creating a form, if you have created a form and you want to access the information that gets posted inside the form, just one thing to keep in mind is that for accessing the info that is posted you must know the username whose info you are about to access), request.headers(it basically returns the HTTP headers).

Now there is a global variable with which you can attach values, and the irony is that its name is actually “g”.
So g is basically a global variable that is used in flask to attach the values.
Now we are gonna talk about another decorator that is provided by flask to us.
The name of this decorator is “errorhandler()”.Now whenever a user experiences an error that we have passed in this decorator (like-errorhandler(500)), it immediately serves us with a corresponding view that we have created with respect to this decorator.

Example:


from flask import Flask, render_template
app = Flask(__name__)
 
 
@app.route('/')
def registration_form():
   return render_template('index1.html')
@app.route('/fun')
def fun():
   return ("Watch animes and chill,you can fill this form some other time.")
@app.route('/fun/<username>')
def show_user(username):
   return f'Hello {username} !'
app.run(debug=True)
 

Now in the above example, you can see that I have created 3 app routes that are giving us different outputs(or views ). 
The first URL basically gives us a registration form as an output, the second one is a static URL that provides us with a simple sentence as an output.
The third one is a dynamic URL as we have to enter the username in the URL to get the proper output.
From above we can see that there is a lot that one can do in flask and in-app routing.

Post a Comment

Previous Post Next Post