2015-11-01 49 views
2

我正在構建一個簡單的Flask應用程序,我想返回重定向響應。另外,我想保持對標題的完全控制。Werkzeug(瓶)響應與重定向不起作用

這是我到目前爲止有:

from flask import Flask 
from werkzeug.wrappers import Response 

app = Flask(__name__) 

@app.route('/toexample') 
def to_example(): 

    headers = { 
      'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0', 
      'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 
      'Accept-Language': 'en-US,en;q=0.5', 
      'Accept-Encoding': 'gzip, deflate', 
    } 

    return Response('www.example.com', status_code=302, headers=headers) 

if __name__ == '__main__': 
    app.run(debug=True) 

我得到一個錯誤:

TypeError: __init__() got an unexpected keyword argument 'status_code' 

好了,它看起來像status_code__init__()不存在,但什麼是正確的方式做到這一點?

我最終要的是一個鏈接,用戶會點擊,但再次,我希望保持對頭

回答

2

我不得不將Location添加到標題。另外,status_code是錯誤的,它應該是status=302

工作例如:

from flask import Flask 
from werkzeug.wrappers import Response 

app = Flask(__name__) 

@app.route('/toexample') 
def to_example(): 

    headers = { 
      'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0', 
      'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 
      'Accept-Language': 'en-US,en;q=0.5', 
      'Accept-Encoding': 'gzip, deflate', 
      'Location': 'http://www.example.com' 
    } 

    return Response('http://www.example.com', status=302, headers=headers) 

if __name__ == '__main__': 
    app.run(debug=True) 
+0

有趣。我沒有想到這一點!看看我的例子。讓我知道你的想法。實際上我對我的解決方案持懷疑態度。有趣... – idjaw

0

有你想兩件事情控制(連接,cookies,引用等)在這裏使用。首先,爲重新引導您想利用redirectto_example電話:

@app.route('/toexample') 
def to_example(): 
    return redirect("http://www.example.com", code=302) 

現在,在自定義頭和cookie的控制,你可以做的是利用after_request這將讓你之後您可以設置某些定製細節一對您的響應對象的請求:

@app.after_request 
def after_request(response): 
    response.headers.add('Custom-Header', 'Custom Header') 
    response.headers.add('Content-Type', 'application/json') 
    response.set_cookie('some-cookie', value='some-cookie-value') 
    return response 

把所有在一起,你比如現在看起來是這樣的:

from flask import Flask, redirect 

app = Flask(__name__) 


@app.route('/toexample') 
def to_example(): 
    return redirect("http://www.example.com", code=302) 


@app.after_request 
def after_request(response): 
    response.headers.add('Custom-Header', 'Custom Header') 
    response.headers.add('Content-Type', 'application/json') 
    response.set_cookie('some-cookie', value='some-cookie-value') 
    return response 

if __name__ == '__main__': 
    app.run(debug=True) 

這是一個捲曲調用。請注意標題。

▶ curl -v http://127.0.0.1:5000/toexample 
* Trying 127.0.0.1... 
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0) 
> GET /toexample HTTP/1.1 
> Host: 127.0.0.1:5000 
> User-Agent: curl/7.43.0 
> Accept: */* 
> 
* HTTP 1.0, assume close after body 
< HTTP/1.0 302 FOUND 
< Content-Type: text/html; charset=utf-8 
< Content-Length: 251 
< Location: http://www.example.com 
< Custom-Header: Custom Header 
< Content-Type: application/json 
< Set-Cookie: some-cookie=some-cookie-value; Path=/ 
< Server: Werkzeug/0.10.4 Python/3.5.0 
< Date: Sun, 01 Nov 2015 23:11:30 GMT 
< 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> 
<title>Redirecting...</title> 
<h1>Redirecting...</h1> 
* Closing connection 0 
<p>You should be redirected automatically to target URL: <a href="http://www.example.com">http://www.example.com</a>. If not click the link.% 
+0

我試過了,但它只是打印'www.example.com'而不是重定向。什麼是「響應」預期是一個字符串? – intelis

+0

@intelis讓我知道你的想法。 – idjaw

+0

感謝您的迴應,儘管我覺得我的方式更清潔:) – intelis

1

這是否把戲對我來說:

from flask import Flask, redirect 

app = Flask(__name__) 


@app.route('/toexample') 
def to_example(): 
    response = redirect("http://www.example.com", code=302) 
    headers = dict(response.headers) 
    headers.update({'X-Custom-Header1': 'value1', 'X-Custom-Header2': 'value2'}) 
    response.headers = headers 
    return response 

這對我的作品上Flaskv0.10.1。乾杯!