使用Flask API进行路由

时间:2019-05-11 17:20:21

标签: python flask routing config

我以前已经构建了此网页,并且可以正常工作。现在,我回去以“正确”的方式进行操作,并停留在此错误的含义上。如果有人有方向,他们可以指出我的方向。

我已经尝试了很多改变代码的组织。我更改了 init 文件的语法以及VIA_ROUTES_MODULE的app.config部分。

结构如下:

\MVC Test
    \app
        __init__.py
        models.py
        oauth.py
        routes.py
        views.py
    run.py

这是我的初始化文件和路线文件。

初始化 .py

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from .models import db


def create_app():
    app = Flask(__name__)
    return app

app = create_app()

db = SQLAlchemy(app)


app.config['VIA_ROUTES_MODULE'] = 'routes'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

from flask.ext.via import Via

via = Via()
via.init_app(app)

routes.py

import views
from flask.ext.via.routers import default, Include

routes = [
    default.Functional('/', views.home),
    default.Functional('/top_played', views.top_played),
    default.Functional('/callback/q', views.callback)
]

这是我得到的全部错误。

/usr/bin/python3.6 /snap/pycharm-community/128/helpers/pydev/pydevd.py --multiproc --qt-support=auto --client 127.0.0.1 --port 36125 --file /home/tony/Documents/Python/MVCTest/run.py
pydev debugger: process 52655 is connecting

Connected to pydev debugger (build 191.7141.48)
/usr/local/lib/python3.6/dist-packages/flask_sqlalchemy/__init__.py:819: UserWarning: SQLALCHEMY_DATABASE_URI not set. Defaulting to "sqlite:///:memory:".
  'SQLALCHEMY_DATABASE_URI not set. Defaulting to '
/usr/local/lib/python3.6/dist-packages/flask_sqlalchemy/__init__.py:839: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
Traceback (most recent call last):
  File "/snap/pycharm-community/128/helpers/pydev/pydevd.py", line 1758, in <module>
    main()
  File "/snap/pycharm-community/128/helpers/pydev/pydevd.py", line 1752, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/snap/pycharm-community/128/helpers/pydev/pydevd.py", line 1147, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/snap/pycharm-community/128/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/tony/Documents/Python/MVCTest/run.py", line 1, in <module>
    from app import app
  File "/home/tony/Documents/Python/MVCTest/app/__init__.py", line 22, in <module>
    via.init_app(app)
  File "/usr/local/lib/python3.6/dist-packages/flask_via/__init__.py", line 188, in init_app
    routes = self.include(routes_module, routes_name)
  File "/usr/local/lib/python3.6/dist-packages/flask_via/__init__.py", line 44, in include
    module = import_module(routes_module)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named 'routes'

Process finished with exit code 1

0 个答案:

没有答案