2014-02-10 324 views
4

我的应用程序中有一个受保护的视图,它只接受POST请求。烧瓶 - login_required +下一个url参数

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

当未经授权的用户试图访问这个观点,我希望他们能够 登录,然后被重定向到这里。

现在,我的登录视图看起来是这样的:

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

那么最终发生的是一个POST请求/预约(这是 “下一个”参数),我得到不被允许的错误。

问题是login()发出GET请求来预约()。我可以通过 解决这个问题,但我不确定如何从/ booking检索原始POST 表单参数?任何想法来解决这个问题?

+0

如果你问如何让这个重定向将用户重定向到回'/ booking'作为POST与原POST的身体,你不能这样做,因为HTTP的一部分。查看[这个问题]的答案(http://stackoverflow.com/questions/46582/response-redirect-with-post-instead-of-get)为什么。 HTTP重定向使用GET方法。这个问题是关于ASP.NET的,但答案总的来说是关于HTTP的。 –

+0

谢谢,我很高兴做出重定向的GET请求,但我不确定如何保留原始POST请求的正文。 – signalseeker

+2

任何你想要的方式。当然,你不能继续使用login_required装饰器,因为它假设你忘记了用户发送的任何东西。您需要将您的视图更改为(如果用户未登录),请保存数据(在会话或数据库中以识别用户会话的方式),然后在用户登录后查看是否有任何“保存”的数据。如果是这样,可以将它们转发到屏幕上显示“您是否想继续此操作”,并使用可以POST的形式并从该数据继续该过程。 –

回答

-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')