简体   繁体   English

类型错误:视图 function 未返回有效响应。 返回类型必须是字符串、字典、元组、Response 实例,但它是一个 int

[英]TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, but it was a int

I have encountered this error while working on my first flask web app.我在处理我的第一个 flask web 应用程序时遇到了这个错误。 In this app, I am trying to get the distance between two points by using Uber H3 and the haversine formula.在这个应用程序中,我试图通过使用 Uber H3 和 haversine 公式来获取两点之间的距离。 I am a beginner in this so any help would be appreciated.:我是这方面的初学者,所以任何帮助将不胜感激。:

TypeError: The view function did not return a valid response.类型错误:视图 function 未返回有效响应。 The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a int.返回类型必须是字符串、字典、元组、响应实例或可调用的 WSGI,但它是一个整数。

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)

The type of the returned variable in flask should be a string, dict or a tuple. flask 中返回变量的类型应该是字符串、字典或元组。 Convert result_distance from an int to a string.将 result_distance 从 int 转换为字符串。 It should eliminate the error.它应该消除错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查看 function 未返回有效响应。 返回类型必须是字符串、字典、元组、响应实例或 WSGI 可调用,但它是一个列表 - 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 list 返回类型必须是字符串、字典、元组、响应实例或可调用的 WSGI,但它是一个 TypeError - The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a TypeError TypeError:返回类型必须是字符串、字典、元组、响应实例或 WSGI 可调用的,但它是协程 - TypeError: The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a coroutine TypeError:视图 function 没有返回有效的响应元组。 元组的形式必须为 (body, status, headers), (body, status), - TypeError: The view function did not return a valid response tuple. The tuple must have the form (body, status, headers), (body, status), 我正面临一个类型错误:视图函数没有返回一个有效的响应。 该函数要么返回 None 要么在没有返回语句的情况下结束 - I'm facing a TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement Flask TypeError:“**”的视图 function 未返回有效响应。 function 要么返回 None 要么在没有返回语句的情况下结束 - Flask TypeError: The view function for '**' did not return a valid response. The function either returned None or ended without a return statement “运行”的视图 function 未返回有效响应。 function 返回 None 或结束时没有返回语句 - The view function for 'run' did not return a valid response. The function either returned None or ended without a return statement 类型错误:返回类型必须是字符串、字典、元组、响应实例或可调用的 WSGI,但它是一个列表 - Type Error: The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a list Flask应用程序中的“视图功能未返回有效响应。”错误在哪里? - Where is the “The view function did not return a valid response.” error in my Flask app? 错误在哪里?“视图函数未返回有效响应。 ”在我的烧瓶应用程序中? - Where is the error “The view function did not return a valid response. ” in my flask app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM