pythonflaskhttp-redirectflask-login

Default query string param called next in redirect for login_required view - Python Flask App


I am trying my hands on Python Flask framework. What I am trying to do is

  1. Have the user authenticated in a form.
  2. If authenticated, show first page with a link to second page

This is a very basic example.

The function which loads the second page is as below

@app.route('/second')
@login_required
def second():
     return render_template('second.html')

As you can notice, the function is protected by @login_required decorator.

Now my question is, when I try to access http://localhost:5000/second and since the function is decorated with @login_required the login view is loaded, which is fine but I see the URL changes from

http://localhost:5000/second to http://localhost:5000/login?next=%2Fsecond

I am not able to understand what this next in url direct. next is an iterator function and what role it has to play in url redirect. I am not able to get a clear understanding of what it is from the flask-login documentation either

Thanks


Solution

  • next is just a name for a URL parameter. The framework that provides the @login_required decorator picked it and uses it to redirect you back once the login succeeds.

    There is nothing special about the name. The framework could have picked origin or return_to or any other name. As long as the login code and the code that generates that URL agree on a name, it doesn't matter how that name is spelled.

    That there is a next() function in Python is pure coincidence.