类型错误:视图函数未返回有效响应。返回类型必须是 string、dict、tuple、Response 实例,但它是 int

发布于 2025-01-10 13:22:04 字数 3720 浏览 0 评论 0原文

我在开发我的第一个 Flask Web 应用程序时遇到了这个错误。在此应用程序中,我尝试使用 Uber H3 和半正矢公式来获取两点之间的距离。我是这方面的初学者,因此我们将不胜感激。:

类型错误:视图函数未返回有效响应。返回类型必须是字符串、字典、元组、Response 实例或可调用的 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)

I have encountered this error while working on my first flask web app. In this app, I am trying to get the distance between two points by using Uber H3 and the haversine formula. I am a beginner in this so any help would be appreciated.:

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.

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懵少女 2025-01-17 13:22:04

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

The type of the returned variable in flask should be a string , dict or a tuple. Convert result_distance from an int to a string. It should eliminate the error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文