Reputation: 3856
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.
Upvotes: 2
Views: 227
Reputation: 3515
The problem is that you're not importing the QuotesView
class. You're import the the QuotesView
module. So in order for this to work correctly, you can do one of two things:
1.Import the class from the module:
from application.controllers.QuotesView import QuotesView
2.Access the class from the import module
from application.controllers import QuotesView
QuotesView.QuotesView.register(app)
Method 1 is a lot cleaner in my opinion.
Upvotes: 1