Flask Value错误视图函数没有返回响应 [英] Flask Value error view function did not return a response

查看:525
本文介绍了Flask Value错误视图函数没有返回响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误消息:
$ b $ pre $ Traceback(最近一次调用最后一次):
文件/ usr / local / lib /python2.7/dist-packages/flask/app.py,第1836行,在__call__
中返回self.wsgi_app(environ,start_response)
文件/usr/local/lib/python2.7 /dist-packages/flask/app.py,第1820行,位于wsgi_app
response = self.make_response(self.handle_exception(e))
文件/usr/local/lib/python2.7 /dist-packages/flask/app.py,行1403,在handle_exception
reraise(exc_type,exc_value,tb)
文件/usr/local/lib/python2.7/dist-packages/ flask / app.py,第1817行,在wsgi_app
response = self.full_dispatch_request()
文件/usr/local/lib/python2.7/dist-packages/flask/app.py第1478行,在full_dispatch_request
response = self.make_response(rv)
文件/usr/local/lib/python2.7/dist-packages/flask/app.py,第1566行,in make_response
提高ValueError('查看函数没有返回响应')
ValueError:查看函数离子没有返回回应

和我的代码:

 #用于启动烧瓶服务器
从烧瓶导入烧瓶#Import烧瓶主管
从烧瓶导入请求#导入烧瓶请求
从烧瓶导入render_template#导入烧瓶渲染模板
导入json,urllib #import api模块
导入时间#Import time in darlek voice
app = Flask(__ name__)

@ app.route('/')
def my_form():
return render_template(my-form.html)#设置渲染模板

@ app.route(' /',methods = ['POST'])
def my_form_post():
openwindow_at = float(request.form ['open'])打开窗口
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开尔文
text = request.form ['text']#从第一页获取信息
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(当前天气+ text ++ data ['weather'] [0] ['description'])#Debug infomation
print(Temp:+ str ['main'] ['temp']))#print temp

if data ['weather'] [0] ['description']。find(rain)> = 0: #Check天气
print(Test)
return 关闭你的窗口
关闭窗口(汤姆的工作)
if float(data ['main'] ['temp'])> = openwindow_at:
return打开窗口
#打开窗口(Tom的工作)
if __name__ =='__main__':
app.debug = True #Uncomment启用调试
app.run()#Run the服务器

my-form.html 是:

 <!DOCTYPE html> 
< html lang =en>
< body>
< h1>输入地点< / h1>
< input type =textname =text>
< input type =submitname =my-formvalue =Send>
< h1>选项< / h1>
< p>如果temp大于< input type =textboxname =open>< / input> < input type =radioname =scalevalue =kelvin> Kelvin< / input>< input type =radioname =scalevalue =celcius> < input type =radioname =scalevalue =fah> FarenHeit< / input>< / p>
< / form>
< / body>
< / html>

如果我在循环中放置加载永远。



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

解决方案

以下内容不返回响应:
$ b

你必须返回类似于 return afunction() return'a string'



这可以解决问题

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:

<!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.

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.

解决方案

The following does not return a response:

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

This can solve the issue

这篇关于Flask Value错误视图函数没有返回响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆