我正在尝试学习如何在 pythonanywhere 上提供的 Flask 应用程序和 mysql 数据库中提供数据。
我添加了一条到 /test 的路由,该路由旨在连接到现有的 mysql 数据库表,然后使用 flask-restless 的 create_api 将其作为 API 提供。
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from flask.ext.cors import CORS
app.config["SQLALCHEMY_DATABASE_URI"] = SomeLoginDetails
app.config["SQLALCHEMY_POOL_RECYCLE"] = 299
db = SQLAlchemy(app)
@app.route("/test")
def create_api():
app.config['CORS_ALLOW_HEADERS'] = "Content-Type"
app.config['CORS_RESOURCES'] = {r"/api/*": {"origins": "*"}}
cors = CORS()
engine = create_engine(SomeLoginDetails)
Base = declarative_base()
Base.metadata.bind = engine
class Prices(Base):
__tablename__ = 'table'
col1 = Column(Integer, primary_key=True)
col2 = Column(Float)
col3 = Column(Float)
col4 = Column(Float)
Base.metadata.create_all()
manager = flask.ext.restless.APIManager(app,
flask_sqlalchemy_db = db)
manager.create_api(Prices, methods=['GET'], max_results_per_page =1000)
return "OK"
如果我离开最后一个 Return "OK",我会收到如下错误:
File "/home/username/.local/lib/python2.7/site-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
但是,如果我添加它,我不会收到任何错误。
这是为什么?
终点在哪里?我试过
http://xxx-sitename-xxx.pythonanywhere.com/test/api
但我收到未找到页面错误。