How to pass GET parameters to url using Flask Request [duplicate]
Asked Answered
B

1

6

I am aware of how to get the request parameters from a url using Flask Request: request.args.get('<param>'). Indeed, that's the only thing I can find when searching for the title question:

I need to know how to send the request parameter for the user's url.

For example, flask_login has a parameter called "next" that stores the url the user was accessing when they accessed a route wrapped in @login_required so that they can be redirected to it after they log in. I have need of this exact functionality with my own forms but I cannot find a way to do implement it.

The relevant section of the flask-login code is here, but I cannot understand how they pass the parameter.

I need a way to record the user's url when they click a link to a form page, pass that as a GET request parameter in their url on the form page, and reaccess it when they submit or cancel the form to return them to their previous page.

Bushy answered 31/8, 2018 at 14:24 Comment(1)
use the redirect functionality, or pass it as a query url as myserver:myport/mypage?arg1=val1&arg2=val2 etcDavie
B
5

Per the documentation I have found the answer (@E.Serra for impetus in the right direction):

flask.url_for(endpoint, **values)

Generates a URL to the given endpoint with the method provided.

Variable arguments that are unknown to the target endpoint are appended to the generated URL as query arguments. If the value of a query argument is None, the whole pair is skipped. In case blueprints are active you can shortcut references to the same blueprint by prefixing the local endpoint with a dot (.).

So if you have the route

@app.route('/view/<variable>/')
def view(variable):
    pass

The call

url_for('view', variable='parameter', variable2='parameter2')

will produce a url where parameter2 is a query argument.

Bushy answered 31/8, 2018 at 15:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.