Home >Backend Development >Python Tutorial >How to Capture Unique Identifiers from Flask Route URLs?
Flask routes often utilize unique identifiers appended to the URL, like in cases such as "landingpageA", "landingpageB", and "landingpageC". How can one access these unique identifiers from within the corresponding Flask route function?
Flask provides support for variable URLs, which allow developers to capture dynamic values from the request URL. To achieve this, you can employ
Using a variable URL, you can access the unique identifier like this:
@app.route('/landingpage<id>') # /landingpageA def landing_page(id): # Here, 'id' will contain the unique identifier from the URL.
Typically, path separators like '/' are used to separate URL components, resulting in the following route definition:
@app.route('/landingpage/<id>') # /landingpage/A def landing_page(id): # Again, 'id' will capture the unique identifier portion of the URL.
Generating URLs with unique identifiers can be achieved using url_for:
url_for('landing_page',>
An alternative approach involves passing the identifier as part of the query string and retrieving it from the request object. However, using variable URLs is generally preferred when the identifier is always required.
Here's an example of using query parameters:
from flask import request @app.route('/landingpage') def landing_page(): id = request.args['id'] # Here, 'id' will be extracted from the query parameter. # Example URL: /landingpage?id=A
The above is the detailed content of How to Capture Unique Identifiers from Flask Route URLs?. For more information, please follow other related articles on the PHP Chinese website!