0

I am developing a website based on Flask and SQLAlchemy

  1. ssis created by sessionmaker in sqlalchemy.orm

  2. 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.

  1. /page/化物语 This shows the page as expected without error.

  2. /page/剑风传奇 This shows the page as expected but with error.

2
  • Can you show the code inside your except block? Commented Dec 19, 2013 at 5:26
  • @alKid I've add try-except clause and also Tracebck msg. Commented Dec 19, 2013 at 5:45

1 Answer 1

3

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()
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry for misleading you, I mean the actor_quotes table is empty, not the whole db. And I can confirm it's not caused by empty table, at least not only: case1: table anime_comments and actor_quotes are empty, but it work without error(i.imgur.com/CvKry0O.png). case2: Almost the same as case1, it turns out error but normal page as expected.
@kxxoling ok but for throwing that error (NoneType has no attribute) the only explanation is the anime object being None. And it happens when no records are found by the query. For whatever reason. :)
Yes, this is the strangest place, cause the page is always rendered successfully, so the anime object can be empty. I guess these lines maybe performed twice, the first time everything is alright, and the second time everything is empty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.