Query Parameters with Multi-Value in Flask A

Query Parameters with Multi-Value in Flask A

Python-based Flask is a micro-framework. It is well-known for creating RESTful APIs since it is lightweight and simple to use. We'll examine how to use multi-value query parameters with flask in this tutorial.


Let's learn basis with following example:


#app.py

from flask import Flask #import library

app = Flask(__name__) #init Flask app


#route

@app.route('/')?

def home():

?return "Hello world"

?

if __name__ == '__main__':

??app.run(host='0.0.0.0', port=5000, debug=True)


To test our app We have to visit below url with the help of web brower:


https://127.0.0.1:5000


This shows "Hello world" text in the browser:

No alt text provided for this image

To get the query parameters in the URL in Flask, use request.args. In order to access and control the query parameters, we can use methods like get, getlist, and to_dict. The request.args property is a dictionary that maps the parameter names to their values. It should be noted that the request.args attribute only works for query parameters and not for URL path parameters.


#app.py

from flask import Flask #import library

app = Flask(__name__) #init Flask app


#route

@app.route('/')?

def home():

?query = request.args.getlist('name')

??print(query)

??return f'Looking for: {query}'

?

if __name__ == '__main__':

??app.run(host='0.0.0.0', port=5000, debug=True)


If we have a url like this:


https://127.0.0.1:5000/?name=samrat&name=rocky


Output will be:

No alt text provided for this image

When a query string has multiple-valued arguments, for example:


https://127.0.0.1:5000/?name=samrat&name=rocky&age=20


In that case, request.args.to_dict() returned value is:

No alt text provided for this image

Even the name query parameters have many values, yet only one value is returned. the default assumption made by Flask is that query parameters have a single value. To return all values for each parameter as a list, we can add flat=False to the to_dict() function.


@app.route('/')

def home():

??query = request.args.to_dict(flat=False)

??print(query)

??return f'Looking for: {query}'


The result of this will be as follows:

No alt text provided for this image

Currently, lists are used to represent all multi-value query parameters

No alt text provided for this image

要查看或添加评论,请登录

Skill Gain的更多文章

  • Do you know how to use Scroll View?

    Do you know how to use Scroll View?

    UIScrollView is a widget or component in UIKit that allows users to scroll through content that is larger than the…

  • Machine Learning

    Machine Learning

    MACHINE LEARNING Machine learning is a field of study that has gained a lot of attention in recent years due to its…

  • JWT Authentication

    JWT Authentication

    Why JWT? When we are working on PHP we can use SESSION for authentication, but when we will use API in a third-party…

  • CSRF Protection in Flask

    CSRF Protection in Flask

    Let's carry out a quick Flask project to demonstrate how you can manually safeguard your data with CSRF protection. In…

  • Dynamic URLs Variable Rule in Flask

    Dynamic URLs Variable Rule in Flask

    Dynamic URLs Variable Rule in Flask This article will go through Python's Flask-Variable Rule. With no database…

  • Introducing the React Hook for Sound Effects "use-sound"

    Introducing the React Hook for Sound Effects "use-sound"

    Introducing the React Hook for Sound Effects "use-sound" Use the React hook use-sound to play sound effects. This is a…

  • How to Create Spinners In Android?

    How to Create Spinners In Android?

    Android Spinner is a view that resembles a drop-down menu and is used to select a single option from a list of options.…

  • Useful Custom Hooks That You Need To Add Into Your React Project (Part II)

    Useful Custom Hooks That You Need To Add Into Your React Project (Part II)

    Useful Custom hooks that You need to add into your React Project : Hooks are great for extracting logic into reusable…

  • A Brief on React Helmet (Part 1)

    A Brief on React Helmet (Part 1)

    React Helmet Every website developer hopes that his or her website will show up first in the browser's search results…

  • Creating Custom cli Commands in Flask

    Creating Custom cli Commands in Flask

    Creating Custom cli Commands in Flask The focus of this post is on using flask to build custom commands. Run is…

社区洞察

其他会员也浏览了