Flask URL Building

In the URL building, we use the  url_for() function to create a URL for a specific function dynamically. As its first argument, it accepts the name of the function and any number of keyword arguments, every corresponding to a variable part of the URL rule. All Unknown parts of the variable are attached as query parameters to the URL.

Flask makes it easy for the user to add the variable part to the URL via the section. We can reuse the variable by adding it as a parameter in the display function. Consider the following

Example

script.py
from flask import *  
app = Flask(__name__)  
@app.route('/admin')
ef admin():
return 'admin'  @app.route('/librarion')  
def librarion():  
return 'librarion'  
@app.route('/student')  
def student():  
return 'student'  
@app.route('/user/<name>')  
def user(name):  
if name == 'admin':  
return redirect(url_for('admin'))  
if name == 'librarion':  
return redirect(url_for('librarion'))  
if name == 'student':  
return redirect(url_for('student'))  
if __name__ =='__main__':  
app.run(debug = True) 

 The  above script has a function user(name), which accepts a value to its argument from the URL address.

The User()  function then checks if an argument received matches ‘admin’ or not. If it matches, the application is redirect to the hello admin() function using url for(), otherwise to the hello guest() function passing the received argument as guest parameter to it.

Flask URL Building

For example, the URL http://localhost:5000/user/admin is redirected to the URL http://localhost:5000/admin, the URL localhost:5000/user/librarion, is redirected to the URL http://localhost:5000/librarion, the URL http://localhost:5000/user/student is redirected to the URL http://localhost/student.

Advantages of  dynamic  URL binding

  • Hard coding of the URLs is avoided
  • Using Dynamic binding,  it  can be  automatically modify the URLs instead of recalling the hard-coded URLs that have been changed manually.
  • URL building handles both the Unicode data and escaping of special characters   in a very transparent manner
  • Generated paths are always absolute in general and, avoiding unexpected behavior of relative paths in browsers.
  • If your application is placed outside the URL root, for example, in /application instead of /, url_for() correctly handles it for you.

Related Topics

Jsonify Flask

Introduction Flask jsonify is a Python feature that allows you to encapsulate a dumps() method and make enhancements to a JSON (JavaScript Object Notation) output to create a response object with...

5 minutes read.

Flask Redirect and Errors

Redirect is also a function provided by the flask. It is called redirect()  function. When this function is called, it returns a response object and also redirects the user to another location with the...

4 minutes read.

Flask Templates

What are templates? Have you ever tried to build a website? Did you encounter the problem that keeps the style of the website consistent, did you have to write the same code several...

5 minutes read.

Python Flask Course

Flask is a web application framework, which is written in python programming language. Armin Ronacher who led a group called poco, was created flask. Flask is having a large community...

4 minutes read.

Flask App Routing

Routing In order to help clients, modern web apps use URLs. Usually One like a website and revisit it if the page uses a easy URL that they can remember and use these...

3 minutes read.

What is flask swagger?

Swagger is a web-based API documentation framework. Users use the Swagger documentation framework to create interactive documents for inbuilt APIs to only work on specific purposes. Swagger documentation has many...

3 minutes read.

Flask Email verification

Email verification We all know that email-verification plays a significant role in today's world for security reasons. In the process of email verification, a random number, text, and combination of both sent to...

4 minutes read.

What is Flask used for?

Flask is a web application framework in python. It doesn’t require any particular tool or a library and doesn’t have a data abstraction layer, but third-party libraries can be added....

3 minutes read.

Flask-Mail Extension

Flask-Mail Extension All web framework provides the basic and advanced features like mail extension. But from the beginning of the tutorial,  we know that flask lies in the category of the micro web...

5 minutes read.

Flask HTTP Methods

Flask HTTP Methods HTTP stands for Hypertext Transfer Protocol that is used to transfer data all over the world wide web(WWW), or we can say how messages are divided (formatted) and transmitted and...

5 minutes read.

Flask File Uploading

Uploading files using the Flask framework is a straightforward task. You need an HTML form with the enctype attribute and the URL manager, which retrieves the file and saves the object in the...

3 minutes read.

Flask Restful

Flask-RESTful is a flask extension used to build REST APIs quickly. Flask-RESTful is a lightweight abstraction that works with existing libraries. It is also helpful for working with minimal setup....

3 minutes read.

Flask SQLite

Flask SQLite Flask allows us to use the SQLite3 which is one of the python modules. It is used for creating database web applications. To understand this, we will be going to create...

10 minutes read.

Flask Cookies

A cookie is a kind of text file that is stored on a client's computer, whose purpose is to remember and track data related to the use of the client to improve...

3 minutes read.

Flask Session

In the session, the data is saved on the server. It can be determined as a time interval in which the client accesses the server until the user logs off. The data...

6 minutes read.

Flask Message Flashing

 Flask Message Flashing All applications and GUI(or the graphic user interfaces) are all about the feedback. If the users do not like the website then they start hating the application. To avoid this situation,...

5 minutes read.

What is a Flask Python?

A Python module that makes it simple to create web applications is called Flask. It is a microframework without an ORM (Object Relational Manager) or similar features and, as such,...

4 minutes read.

GitHub Pages Python Flask

Python Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Flask URL Building

In the URL building, we use the  url_for() function to create a URL for a specific function dynamically. As its first argument, it accepts the name of the function and any number...

2 minutes read.

Flask Request Object

The flask object has a significant role to play because it is the request object that holds the data when the data is sent form an html form in the form of information...

3 minutes read.