【问题标题】:Flask Value error view function did not return a response [duplicate]烧瓶值错误视图函数没有返回响应[重复]
【发布时间】:2014-09-21 22:27:15
【问题描述】:

错误信息:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1478, in full_dispatch_request
    response = self.make_response(rv)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1566, in make_response
    raise ValueError('View function did not return a response')
ValueError: View function did not return a response

还有我的代码:

#Used for starting the flask server
from flask import Flask #Import flask mains
from flask import request #Import flask requests
from flask import render_template #Import flask render templates
import json, urllib #import api modules
import time #Imporing time in darlek voice
app = Flask(__name__)

@app.route('/')
def my_form():
    return render_template("my-form.html") #Set render template

@app.route('/', methods=['POST'])
def my_form_post():
    openwindow_at = float(request.form['open']) #When it reached ? open window
    if request.form['scale'] == "kelvin": #Change figures
        print("Do nothing") #Debug info
    elif request.form['scale'] == "celcius":
        openwindow_at = openwindow_at + int(273.15) #celcius to kelvin
    elif request.form['scale'] == "fah": #Fah to kelvin
        openwindow_at = (openwindow_at + 459.67) * 5 / 9  #F to kelvin
    text = request.form['text'] #Get info from First page
    url = "http://api.openweathermap.org/data/2.5/weather?q=" + text #Download the json
    response = urllib.urlopen(url) #Download Json
    data = json.loads(response.read()) #Parse json
    print("Current Weather in " + text + " " + data['weather'][0]['description']) #Debug infomation
    print("Temp: " + str(data['main']['temp'])) #Print temp 

    if data['weather'][0]['description'].find("rain") >= 0: #Check The weather
        print("Test")
        return "Shutting your window"
        Close the window(Tom's job)
    if float(data['main']['temp']) >= openwindow_at:
        return "Opening your window"
        #open the window (Tom's job)
if __name__ == '__main__':
    app.debug = True #Uncomment to enable debugging
    app.run() #Run the Server

my-form.html 是:

<!DOCTYPE html>
<html lang="en">
<body>
    <h1>Enter a place</h1>
    <form action="." method="POST">
        <input type="text" name="text">
        <input type="submit" name="my-form" value="Send">
    <h1>Options</h1>
    <p>Open window if temp is greater than<input type="textbox" name="open"></input> <input type="radio" name="scale" value="kelvin">Kelvin</input><input type="radio" name="scale" value="celcius">Celcius</input><input type="radio" name="scale" value="fah">FarenHeit</input></p>
    </form>
</body>
</html>

如果我在其中放置一个 while 循环,它将永远加载。

然后输入一个比页面永远加载的当前温度更高的温度。 如果我使用上面列出的当前代码,它会给出错误。

【问题讨论】:

  • 这不是重复的,因为这里的请求是一个 POST,返回一些东西是不直观的,而在“重复”中,它是一个 GET,很明显有一个错误。恕我直言,这是烧瓶中的设计问题,我通常在发布路线结束时使用“返回”来修复它
  • 为什么 POST 不返回任何东西?
  • 当我显式定义 request.method == 'GET' 的返回值并使用 HEAD 调用路由时遇到了这个问题。我修复了这个问题,改为检查request.method in ['HEAD', 'GET']

标签: python flask


【解决方案1】:

您没有从您的视图my_form_post 返回响应对象。函数以隐含的return None 结尾,这是 Flask 不喜欢的。

例如让函数my_form_post返回显式响应

return 'OK'

在函数的末尾。

【讨论】:

  • +1 : 函数以隐式返回 None 结束,Flask 不喜欢这 是我遗漏的一点(一个裸露的return 也是问题所在)。 return '' 很好,所以我不确定 Flask 是否明确喜欢 return None 或者是否还有其他值(因为 '' 很好,它不是任何虚假值)
  • 不带return语句或不带参数的return退出函数在Python中返回None,调用者根本无法区分它们和return None
  • 恭喜获得 100k。
  • @chux-ReinstateMonica 谢谢
【解决方案2】:

以下内容不返回响应:

您必须返回 return afunction()return 'a string' 之类的任何内容。

这样可以解决问题

【讨论】:

    猜你喜欢
    • 2017-08-24
    • 2020-02-08
    • 2015-01-07
    • 2022-01-22
    • 2020-01-10
    • 1970-01-01
    • 2020-09-16
    相关资源
    最近更新 更多