0

I'm trying to manipulate the following code to issue a 307 redirect, instead of the default 302. Code:

@app.route('/', methods=('GET', 'POST'))
def home():
    if request.method == 'POST':
        username = request.form.get('username')
        user = User.query.filter_by(username=username).first()
        if not user:
            user = User(username=username)
            db.session.add(user)
            db.session.commit()
        session['id'] = user.id
        return redirect('/',code = 307)
    user = current_user()
    return render_template('home.html', user=user) 

The functionality of the above code is after a user enters the username, redirect to the home page.

Although it's issued properly (by inspecting the according message in terminal), I keep getting a "This page isn't redirecting properly" from the browser. The same code works perfectly for the simple redirect

return redirect('/')

What am I missing ?

viajero cósmico
  • 75
  • 1
  • 1
  • 7
  • I'm not sure why you're getting this problem. You should try printing the redirect before returning it (or inspecting in a debugger). You might want to consider creating a `/login` route, and redirect to `"/login?next=" + request.full_path` to log the user in.. – thebjorn Nov 07 '16 at 10:24
  • 1
    Ah, the reason for your problem is that with a 307, the user agent "MUST NOT change the request method if it performs an automatic redirection". Meaning that your redirect will also be a POST and you have infinite recursion. – thebjorn Nov 07 '16 at 10:26
  • @thebjorn You are right! Now I see the point. I surpassed the problem by creating a middle redirection to another route. Thank you ! – viajero cósmico Nov 07 '16 at 10:56

0 Answers0