我正在開發基於瓶和SQLAlchemy的網站奇怪AttributeError的基於網站
ss
由sessionmaker創造sqlalchemy.orm動漫,ActorQuote,AnimeComment是基於數據庫模型結構
```
@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)
如例外,它會返回一個包含這些參數的頁面 - 動畫,actor_quotes,anime_comments,它也會,但也會拋出一個屬性錯誤。
```
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'
當我使用try - 除了捕獲錯誤,它會引發另一UnboundLocalError。
```
什麼是更奇怪的是該網站仍然有效,以及什麼都沒有發生。
/page/化物語這顯示了預期沒有錯誤的頁面。
/page/劍風傳奇這顯示了預期的頁面,但有錯誤。
你可以在你的except塊內顯示代碼嗎? – aIKid
@alKid我已經添加了try-except從句,還有Tracebck msg。 – kxxoling