尝试使用蓝图将路由装饰器移出主脚本
Attempting to move route decorators out of main script using a bluerint
我正在使用 PythonAnywhere 免费版尝试为应用程序设置一个小型休息服务。我也在使用带有蓝图的烧瓶来尝试这样做。
我的文件结构如下所示
mysite/
applicaction/
__init__.py,
models.py,
config.py
flask_app.py(与mysite包同级)
我使用 flask_app.py 到 运行 下面显示的服务器。
from application import __init__ as appmain
if __name__ == "__main__":
myapp = appmain.getapp()
myapp.run()
在我的应用程序文件夹中,我使用“init.py”来设置我的 "app" 实例以及名为 "myblueprint" 的蓝图(代码显示下)
from flask import Flask, Blueprint
from flask.ext.sqlalchemy import SQLAlchemy
db = None
app = None
serviceblueprint = None
def init_app():
app = Flask(__name__)
app.config.from_object('config')
serviceblueprint = Blueprint('serviceblueprint', __name__)
app.register_blueprint(serviceblueprint)
db = SQLAlchemy(app)
def get_app():
init_app()
return app
然后我尝试使用已导入 "serviceblueprint" 的 service.py 脚本中的注册蓝图定义一个简单的路由装饰器。理想的 id 像 serive.py 模块包含一个 class 可以处理路由(不确定这是否可能)但是当我到达它时我会穿过那座桥。现在它是一个基本脚本(如下所示)
from application import serviceblueprint
@serviceblueprint.route('/')
def get():
return "<h1>Yahoo!!!</h1>"
不幸的是,当我将主 url 加载到浏览器中时,我只是收到一个错误页面,以及一个我在服务器上不理解的相对通用的跟踪日志。它似乎至少没有指向我的代码中的任何内容。也许 "ImportError: cannot import name app" 除外,但我不会在我的任何脚本中导入 "app"。
2015-10-13 09:46:59,068 :Traceback (most recent call last):
2015-10-13 09:46:59,068 : File "/bin/user_wsgi_wrapper.py", line 134, in __call__
2015-10-13 09:46:59,068 : self.error_log_file.logger.exception("Error running WSGI application")
2015-10-13 09:46:59,068 : File "/usr/lib/python2.7/logging/__init__.py", line 1185, in exception
2015-10-13 09:46:59,069 : self.error(msg, *args, **kwargs)
2015-10-13 09:46:59,069 : File "/usr/lib/python2.7/logging/__init__.py", line 1178, in error
2015-10-13 09:46:59,069 : self._log(ERROR, msg, args, **kwargs)
2015-10-13 09:46:59,069 : File "/usr/lib/python2.7/logging/__init__.py", line 1270, in _log
2015-10-13 09:46:59,069 : record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
2015-10-13 09:46:59,070 : File "/usr/lib/python2.7/logging/__init__.py", line 1244, in makeRecord
2015-10-13 09:46:59,070 : rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
2015-10-13 09:46:59,070 : File "/usr/lib/python2.7/logging/__init__.py", line 284, in __init__
2015-10-13 09:46:59,070 : self.threadName = threading.current_thread().name
2015-10-13 09:46:59,070 : File "/usr/lib/python2.7/threading.py", line 1160, in currentThread
2015-10-13 09:46:59,070 : return _active[_get_ident()]
2015-10-13 09:46:59,070 : File "/bin/user_wsgi_wrapper.py", line 126, in __call__
2015-10-13 09:46:59,071 : app_iterator = self.app(environ, start_response)
2015-10-13 09:46:59,071 : File "/bin/user_wsgi_wrapper.py", line 140, in import_error_application
2015-10-13 09:46:59,071 : raise e
2015-10-13 09:46:59,071 :ImportError: cannot import name app
如果有人能帮助我,我将不胜感激。我也是 Flask 的新手,所以很明显我可能会犯一个非常简单的错误。谢谢!
如果您查看当前托管的所有网站所在的 webapps 选项卡,您将看到一个 wsgi.py 文件。在其中,您会看到它尝试从您的项目文件中导入 app
。
确保它能够成功执行此操作。
我正在使用 PythonAnywhere 免费版尝试为应用程序设置一个小型休息服务。我也在使用带有蓝图的烧瓶来尝试这样做。 我的文件结构如下所示
mysite/
applicaction/
__init__.py,
models.py,
config.py
flask_app.py(与mysite包同级)
我使用 flask_app.py 到 运行 下面显示的服务器。
from application import __init__ as appmain
if __name__ == "__main__":
myapp = appmain.getapp()
myapp.run()
在我的应用程序文件夹中,我使用“init.py”来设置我的 "app" 实例以及名为 "myblueprint" 的蓝图(代码显示下)
from flask import Flask, Blueprint
from flask.ext.sqlalchemy import SQLAlchemy
db = None
app = None
serviceblueprint = None
def init_app():
app = Flask(__name__)
app.config.from_object('config')
serviceblueprint = Blueprint('serviceblueprint', __name__)
app.register_blueprint(serviceblueprint)
db = SQLAlchemy(app)
def get_app():
init_app()
return app
然后我尝试使用已导入 "serviceblueprint" 的 service.py 脚本中的注册蓝图定义一个简单的路由装饰器。理想的 id 像 serive.py 模块包含一个 class 可以处理路由(不确定这是否可能)但是当我到达它时我会穿过那座桥。现在它是一个基本脚本(如下所示)
from application import serviceblueprint
@serviceblueprint.route('/')
def get():
return "<h1>Yahoo!!!</h1>"
不幸的是,当我将主 url 加载到浏览器中时,我只是收到一个错误页面,以及一个我在服务器上不理解的相对通用的跟踪日志。它似乎至少没有指向我的代码中的任何内容。也许 "ImportError: cannot import name app" 除外,但我不会在我的任何脚本中导入 "app"。
2015-10-13 09:46:59,068 :Traceback (most recent call last):
2015-10-13 09:46:59,068 : File "/bin/user_wsgi_wrapper.py", line 134, in __call__
2015-10-13 09:46:59,068 : self.error_log_file.logger.exception("Error running WSGI application")
2015-10-13 09:46:59,068 : File "/usr/lib/python2.7/logging/__init__.py", line 1185, in exception
2015-10-13 09:46:59,069 : self.error(msg, *args, **kwargs)
2015-10-13 09:46:59,069 : File "/usr/lib/python2.7/logging/__init__.py", line 1178, in error
2015-10-13 09:46:59,069 : self._log(ERROR, msg, args, **kwargs)
2015-10-13 09:46:59,069 : File "/usr/lib/python2.7/logging/__init__.py", line 1270, in _log
2015-10-13 09:46:59,069 : record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
2015-10-13 09:46:59,070 : File "/usr/lib/python2.7/logging/__init__.py", line 1244, in makeRecord
2015-10-13 09:46:59,070 : rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
2015-10-13 09:46:59,070 : File "/usr/lib/python2.7/logging/__init__.py", line 284, in __init__
2015-10-13 09:46:59,070 : self.threadName = threading.current_thread().name
2015-10-13 09:46:59,070 : File "/usr/lib/python2.7/threading.py", line 1160, in currentThread
2015-10-13 09:46:59,070 : return _active[_get_ident()]
2015-10-13 09:46:59,070 : File "/bin/user_wsgi_wrapper.py", line 126, in __call__
2015-10-13 09:46:59,071 : app_iterator = self.app(environ, start_response)
2015-10-13 09:46:59,071 : File "/bin/user_wsgi_wrapper.py", line 140, in import_error_application
2015-10-13 09:46:59,071 : raise e
2015-10-13 09:46:59,071 :ImportError: cannot import name app
如果有人能帮助我,我将不胜感激。我也是 Flask 的新手,所以很明显我可能会犯一个非常简单的错误。谢谢!
如果您查看当前托管的所有网站所在的 webapps 选项卡,您将看到一个 wsgi.py 文件。在其中,您会看到它尝试从您的项目文件中导入 app
。
确保它能够成功执行此操作。