How do I access named parameters in Flask URLs?

Mary-Kate Olsen
Release: 2024-10-29 14:09:02
Original
447 people have browsed it

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
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template