我的文件夹根app.py是这样的
import application
if __name__ == '__main__':
application.app.run()
我有一个文件夹叫应用程序与__init__.py
和三个文件夹:控制器,模型和视图。
__init__.py
看起来像这样
__version__ = '0.1'
from application.controllers import QuotesView
from flask import Flask
app = Flask('application')
QuotesView.register(app)
我的controllers
文件夹有两个文件__init__.py
和QuotesView.py
如下所示:QuotesView.py
from flask.ext.classy import FlaskView, route
# we'll make a list to hold some quotes for our app
quotes = [
"A noble spirit embiggens the smallest man! ~ Jebediah Springfield",
"If there is a way to do it better... find it. ~ Thomas Edison",
"No one knows what he can do till he tries. ~ Publilius Syrus"
]
class QuotesView(FlaskView):
@route('/')
def index(self):
return "<br>".join(quotes)
def before_request(self, name):
print("something is happening to a widget")
def after_request(self, name, response):
print("something happened to a widget")
return response
和__init__.py
看起来像这样:
import os
import glob
__all__ = [os.path.basename(
f)[:-3] for f in glob.glob(os.path.dirname(__file__) + "/*.py")]
当我运行python app.py
时,我得到一个属性缺失错误:
Traceback (most recent call last):
File "app.py", line 2, in <module>
import application
File "/home/ace/flask/application/__init__.py", line 5, in <module>
QuotesView.register(app)
AttributeError: 'module' object has no attribute 'register'
我似乎不知道错误在哪里,虽然我觉得这是我的进口。我对python很陌生,所以它可能很简单。
问题是您没有导入QuotesView
类。您正在导入QuotesView
模块。为了使它正常工作,你可以做两件事之一:
1。从模块中导入类:
from application.controllers.QuotesView import QuotesView
2。从导入模块
访问类from application.controllers import QuotesView
QuotesView.QuotesView.register(app)