My folder root app.py looks like this
import application
if __name__ == '__main__':
application.app.run()
I have a folder called application with __init__.py
and three folders: controllers, models and views.
The __init__.py
Looks like this
__version__ = '0.1'
from application.controllers import QuotesView
from flask import Flask
app = Flask('application')
QuotesView.register(app)
My controllers
folder has two files __init__.py
and QuotesView.py
as shown below:
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
And __init__.py
looks like this:
import os
import glob
__all__ = [os.path.basename(
f)[:-3] for f in glob.glob(os.path.dirname(__file__) + "/*.py")]
When I run python app.py
I get an attribute missing error:
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'
I cant seem to figure out where the error is though i feel its my importing. I am pretty new to python, so it might be something simple.
答案 0 :(得分:1)
问题是您没有导入QuotesView
课程。您导入了from application.controllers.QuotesView import QuotesView
模块。因此,为了使其正常工作,您可以执行以下两项操作之一:
1.从模块中导入课程:
from application.controllers import QuotesView
QuotesView.QuotesView.register(app)
2.从导入模块访问该类
array1 = [{a: '1', b:'2', c:'3'}, {a: '4', b: '5', c:'6'}]
array2 = [{a: '1', b:'2', c:'10'}, {a: '3', b: '5', c:'6'}]
在我看来,方法1更清洁。