M/s VIKASH TECH - We Write Imaginations to Codes..!!

FLASK ROUTING

Let me introduce you to the routing concepts of the Flask framework. Routing is a major and basic part of this framework as it defies which data has to be forwarded to the client on different URL requests.
Moving to the depth of routing, route() is called the decorator in Flask, which is used to bind URL to functions as shown below.

Python Code

@app.route(‘/hello’)
def hello_world():
   return ‘hello world’

In the above line of codes, the URL ‘/hello‘ rule is bounded to the hello_world() function, due to which if a client requests for the hello page using URL – http://localhost:5000/hello  an output of “hello world” is displayed on the browser.
The add_url_rule() function of an application object is also available to bind a URL with a function as in the above example, route() is used.
Also, use of add_url_route() function can be done to bind a URL with a function as done by route() function in above example. How?? Let’s see.

Python Code

def hello_world():
   return ‘hello world’
app.add_url_rule(‘/’, ‘hello’, hello_world)

Here also, the same thing is being performed, the difference is, you do not need to define the function just below the decorator, you can define the functions first and then add the URL rules at the end, one after another. It helps to keep the code a little cleaner.
Are you getting interested?? There’s a lot to go.. stay tuned…

Category :

Comments

No comments found.

….

FLAT 50% OFF

Latest Posts

SuiteCRM, an open source tool for startups to use from an early stage.

When we talk about businesses in their early stage, it's quite a big deal to choose good management and sales…

How to Implement Google QAuth Authentication | Google Login - PHP MySQL

PHP Google OAuth API allows users to login into a website with their Google credentials. A user with a Google…

How to use Google Two Factor Authentication in your project?

At our end we take security at a priority, implementation of different ways to secure website should be the priority…

How to verify an email address before sending emails?

Email marketing is a great way to reach your clients, but while sending emails from a mail service provider, its…

%d bloggers like this: