1

我正在尝试学习如何在 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

但我收到未找到页面错误。

4

2 回答 2

3

此外,设置您的应用程序和数据库模型并在视图中创建 API 似乎真的很奇怪。

于 2016-05-16T16:14:35.063 回答
2

对于第一个问题:

原因很清楚,你的视图函数应该返回一个响应,它可以是一个 str、unicode、WSGI 对象或一个元组。

对于第二个问题:

如果你想知道flask中的endpoint是什么,你可以看到这个问题:What is an 'endpoint' in Flask?. 你得到NotFound错误的原因是你没有定义/test/api路线,你只有/test路线。

于 2016-05-16T12:33:23.343 回答