当前位置: 首页 > 知识库问答 >
问题:

TypeError:视图函数没有返回有效的响应。返回类型必须是字符串、推文、元组、响应实例,但它是一个int

饶谦
2023-03-14

我在处理我的第一个烧瓶Web应用程序时遇到了此错误。在这个应用程序中,我试图通过使用Uber H3和haversine公式来获得两点之间的距离。我是这方面的初学者,所以任何帮助将不胜感激。

类型错误:视图函数未返回有效响应。返回类型必须是字符串、字典、元组、响应实例或 WSGI 可调用,但它是一个 int。

Traceback (most recent call last)
File "/Users/shipsy/Documents/flask_tutorial/venv/lib/python3.7/site-packages/flask/app.py", line 2091, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/shipsy/Documents/flask_tutorial/venv/lib/python3.7/site-packages/flask/app.py", line 2076, in wsgi_app
response = self.handle_exception(e)
File "/Users/shipsy/Documents/flask_tutorial/venv/lib/python3.7/site-packages/flask/app.py", line 2073, in wsgi_app
response = self.full_dispatch_request()
File "/Users/shipsy/Documents/flask_tutorial/venv/lib/python3.7/site-packages/flask/app.py", line 1519, in full_dispatch_request
return self.finalize_request(rv)
File "/Users/shipsy/Documents/flask_tutorial/venv/lib/python3.7/site-packages/flask/app.py", line 1538, in finalize_request
response = self.make_response(rv)
File "/Users/shipsy/Documents/flask_tutorial/venv/lib/python3.7/site-packages/flask/app.py", line 1731, in make_response
"The view function did not return a valid"
TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a int.
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
import h3
import googlemaps
from haversine import haversine

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

class ETAcalculation(db.Model):
    SNo = db.Column(db.Integer, primary_key=True)
    origin = db.Column(db.String(16), nullable = False)
    destination = db.Column(db.String(16), nullable = False)
    mode = db.Column(db.String(16), default = 'walking')
    # eta = db.Column(db.Integer)
    distance = db.Column(db.Integer)

    def __repr__(self):
        return self.distance
    
@app.route("/", methods = ['GET', 'POST'])
def hello_world():
    if request.method == 'POST':
        origin_hex_id = request.form['origin']
        destination_hex_id = request.form['destination']
        travelMode = request.form['mode']
        origin_coordinates = h3.h3_to_geo(origin_hex_id)
        destination_coordinates = h3.h3_to_geo(destination_hex_id)
        if (h3.h3_is_valid(origin_hex_id) and h3.h3_is_valid(destination_hex_id)):
            available = ETAcalculation.query.filter_by(origin = origin_hex_id, destination = destination_hex_id, mode = travelMode).first()
            reverse_available = ETAcalculation.query.filter_by(origin = destination_hex_id, destination = origin_hex_id, mode = travelMode).first()
            if available is None and reverse_available is not None:
                return reverse_available.distance
            if available is not None and reverse_available is None:
                return available.distance
            if available is None and reverse_available is None:
                result_distance = int(haversine(origin_coordinates, destination_coordinates))
                new_record = ETAcalculation(origin = origin_hex_id, destination = destination_hex_id, mode = travelMode, distance = result_distance)
                return result_distance
                try:
                    db.session.add(new_record)
                    db.session.commit()
                except:
                    "There was an error encountered in adding the details"
    else:
        return render_template('index.html')


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

共有1个答案

尹昂雄
2023-03-14

flask中返回变量的类型应为字符串、dict或元组。将result_distance从int转换为字符串。它应该消除错误。

 类似资料:
  • 我正在使用flask和MongoDB创建和启动AWS EC2实例,并为不同用户记录这些实例。但当我试图调用我编写的用于检索文档的函数时,会返回以下错误: TypeError:视图函数没有返回有效的响应。返回类型必须是字符串、字典、元组、响应实例或WSGI callable,但它是一个协程。 追溯: 我的数据库代码是: 我从调用的函数是: 我不确定这些数据是否足以解决这个错误,但是任何帮助都将不胜感

  • 问题内容: 这是我自制的序列化课程: 这是网络方法 Ajax调用客户端 最后,我的回调方法 rsp.d是包含所有属性的字符串…属性本身是未定义的。我知道我在这里缺少一些简单的东西。 从服务器返回的字符串 问题答案: 您不应该手动序列化返回值。ASP.NET将为您做到这一点。尝试这样的事情: 如果您需要返回更动态的内容(例如您的示例似乎正在做的事情),则可以使用匿名类型:

  • 很久以前,我在一个论坛上偶然发现了一个有趣的问题,我想知道答案。 请考虑以下 C 函数: 这应该总是返回< code>false,因为< code>var3 == 3000。< code>main函数如下所示: 由于 应始终返回 ,因此期望程序仅将一个 false 打印到屏幕上。但是在编译并运行它之后,还会显示执行: 这是为什么?这段代码有某种未定义的行为吗? 注意:我用

  • 嗨,我正在尝试使用烧瓶创建水平条形图,并面临以下错误。类型错误:视图函数未返回有效响应。该函数要么返回 None,要么在没有 return 语句的情况下结束。请帮忙。提前致谢。 来自flask的app.py导入Flask,重定向,url_for,render_template,请求,flash,会话导入熊猫as PD app = Flask(name)app . debug = True samp

  • 以下代码段将文件发送到浏览器。 如果我想在发送文件之前打印一些行,比如: 如果我尝试添加 pdata 回复,如: 我得到一个错误,return应该只返回字符串而不是字符串响应