Flask - login_required + next url params

I have a protected view in my application that just accepts POST requests.

@app.route("/booking", methods=("POST", ))
@login_required
def booking():
     arg1 = request.form.get("arg1")
     arg2 = request.form.get("arg2")

When an unauthorized user tries to access this view, I want them to log in and then redirect here.

Currently, my login window is as follows:

@app.route("/login", methods=("GET", "POST"))
@login_required
def login():
     do_login()
     return redirect(request.args.get('next') or url_for('home'))

So what happens is a POST / reservation request (which is the “next” parameter), and I get the error DO NOT ALLOW.

The problem is that login () makes a GET request for booking (). I can get around this, but I'm not sure how to get the original POST enter the arguments from / booking? Any ideas to get around this?

+4
1

POST ? , , GET.

@app.route("/booking", methods=['GET','POST'])
@login_required
def booking():
    # render the form. something like
    form = BookingForm()

    # Check if POST
    if request.method == 'POST':
        # process the form now and do whatever you need. 
        return redirect(url_for('index'))

    # code below will run if not POST. You should render the template here
    return render_templte('booking.html')
-1

Source: https://habr.com/ru/post/1526201/


All Articles