Reputation: 4268
I am developing a website based on Flask and SQLAlchemy
ss
is created by sessionmaker in sqlalchemy.orm
Anime, ActorQuote, AnimeComment are models based on database structure
```
@app.route('/page/<anime_name>', methods=['GET', 'POST'])
def show_page(anime_name):
if request.method == 'GET':
anime = ss.query(Anime).filter_by(anime_name=anime_name).first()
actor_quotes = ss.query(ActorQuote).filter_by(anime_id=anime.id).all()
anime_comments = ss.query(AnimeComment).filter_by(anime_id=anime.id).all()
return render_template('page.html', anime=anime, actor_lines=actor_quotes, comments=anime_comments)
As excepted, it would return a page with these arguments -- anime, actor_quotes, anime_comments, and it does, but also throw a Attribute Error.
```
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Library/Python/2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/windrunner/hiacg/index.py", line 138, in show_page
actor_lines = ss.query(ActorQuote).filter_by(anime_id=anime.id).all()
AttributeError: 'NoneType' object has no attribute 'id'
When I use try-except to catch the error, it throws another UnboundLocalError.
```
try:
anime = ss.query(Anime).filter_by(anime_name=anime_name).first()
actor_quotes = ss.query(ActorQuote).filter_by(anime_id=anime.id).all()
anime_comments = ss.query(AnimeComment).filter_by(anime_id=anime.id).all()
except AttibuteError:
print 'AttibuteError occurs again!'
What is more strange is the website still works well as nothing happened.
/page/化物语 This shows the page as expected without error.
/page/剑风传奇 This shows the page as expected but with error.
Upvotes: 0
Views: 237
Reputation: 8202
In your line
anime = ss.query(Anime).filter_by(anime_name=anime_name).first()
the variable anime
is None
if not found (as you said in database empty) and thus the AttributeError
when trying to access None
.id
.
Btw I kindly suggest you to use Flask-SQLAlchemy extension that tackles all the db connection logic and exposes some useful functions such as `.first_or_404'. With it your code would become
anime = Anime.query.filter_by(anime_name=anime_name).first_or_404()
Upvotes: 3