[英]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.