Home >Backend Development >Python Tutorial >How do I access named parameters in Flask URLs?

How do I access named parameters in Flask URLs?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-29 14:09:02484browse

How do I access named parameters in Flask URLs?

Accessing Named Parameters in Flask URLs

In Flask, handling named parameters in a URL is essential for extracting key-value pairs from requests.

Scenario:

Consider a Flask endpoint for user login:

http://10.1.1.1:5000/login?username=alex&password=pw1

Objective:

  • Retrieve the named parameters (username and password) from the URL.

Solution:

Flask provides a powerful tool, request.args, to access named parameters from the query string:

<code class="python">from flask import request

@app.route('/login')
def login():
    username = request.args.get('username')
    password = request.args.get('password')</code>

Now, username and password hold the extracted parameter values.

Note:

  • request.args is a dictionary-like object containing all query string parameters.
  • request.args.get() retrieves a specific parameter value by key (parameter name).

The above is the detailed content of How do I access named parameters in Flask URLs?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn