Flask Value错误视图函数未返回响应[重复]

[英]Flask Value error view function did not return a response [duplicate]


This question already has an answer here:

这个问题在这里已有答案:

Error message:

错误信息:

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

And my code:

我的代码:

#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

and my-form.html is:

和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>

If I put a while loop there it loads for ever.

如果我把一个while循环放在那里它永远加载。

Then enter a temp higher than the current one the page loads forever. And if I use the current code listed above it gives the error.

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

2 个解决方案

#1


57  

The following does not return a response:

以下内容未返回响应:

You must return anything like return afunction() or return 'a string'.

你必须返回return ifunction()或返回'string'之类的东西。

This can solve the issue

这可以解决问题

#2


19  

You are not returning a response object from your view my_form_post. The function ends with implicit return None, which Flask does not like.

您没有从视图my_form_post返回响应对象。该函数以隐式返回None结束,Flask不喜欢。

Make the function my_form_post return an explicit response, for example

例如,使函数my_form_post返回显式响应

return 'OK'

at the end of the function.

在功能的最后。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2014/07/30/d4afc328a610aebda3ae400677222c51.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号