Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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:

  • How do you get a query string on Flask?
  • How to pass a URL parameter using python, Flask, and the command line

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.

like image 721
mas Avatar asked Aug 31 '18 14:08

mas


1 Answers

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.

like image 176
mas Avatar answered Oct 11 '22 15:10

mas