How do I pass through the "next" URL with Flask and Flask-login?
<p>The documentation for <a href="https://flask-login.readthedocs.org/en/latest/" rel="noreferrer">Flask-login</a> talks about handling a "next" URL.  The idea seems to be:</p>

<ol>
<li>User goes to <code>/secret</code></li>
<li>User is redirect to a login page (e.g. <code>/login</code>)</li>
<li>After a successful login, the user is redirected back to <code>/secret</code></li>
</ol>

<p>The only semi-full example using Flask-login that I've found is <a href="https://gist.github.com/bkdinoop/6698956" rel="noreferrer">https://gist.github.com/bkdinoop/6698956</a> .  It's helpful, but since it doesn't include the HTML template files, I'm seeing if I can recreate them as an self-training exercise.</p>

<p>Here's a simplified version of the <code>/secret</code> and <code>/login</code> section:</p>

<pre><code>@app.route("/secret")
@fresh_login_required
def secret():
    return render_template("secret.html")

@app.route("/login", methods=["GET", "POST"])
def login():
    &lt;...login-checking code omitted...&gt;
    if user_is_logged_in:
        flash("Logged in!")
        return redirect(request.args.get("next") or url_for("index"))
    else:
        flash("Sorry, but you could not log in.")
        return render_template("login.html")
</code></pre>

<p>And here's <code>login.html</code>:</p>

<pre><code>&lt;form name="loginform" action="{{ url_for('login') }}" method="POST"&gt;
Username: &lt;input type="text" name="username" size="30" /&gt;&lt;br /&gt;
Password: &lt;input type="password" name="password" size="30" /&gt;&lt;br /&gt;
&lt;input type="submit" value="Login" /&gt;&lt;br /&gt;
</code></pre>

<p>Now, when the user visits <code>/secret</code>, he gets redirected to <code>/login?next=%2Fsecret</code> .  So far, so good - the "next" parameter is in the query string.</p>

<p>However when the user submits the login form, he is redirected back to the index page, not back to the <code>/secret</code> URL.  </p>

<p>I'm guessing the reason is because the "next' parameter, which was available on the incoming URL, is not incorporated into the login form and is therefore not being passed as a variable when the form is processed.  But what's the right way to solve this?</p>

<p>One solution seems to work - change the <code>&lt;form&gt;</code> tag from</p>

<pre><code>&lt;form name="loginform" action="{{ url_for('login') }}" method="POST"&gt;
</code></pre>

<p>to:</p>

<pre><code>&lt;form name="loginform" method="POST"&gt;
</code></pre>

<p>With the "action" attribute removed, the browser (at least Firefox 45 on Windows) automatically uses the current URL, causing it to inherit the <code>?next=%2Fsecret</code> query string, which successfully sends it on to the form processing handler.</p>

<p>But is omitting the "action" form attribute and letting the browser fill it in the right solution?  Does it work across all browsers and platforms?</p>

<p>Or does Flask or Flask-login intend for this to be handled in a different way?</p>