前提・実現したいこと
Head First PythonでPythonを勉強しています。
関数デコレーターを使って、URLへのアクセスを
制限したりしようとしています。
一通りコードを書き終わり、実行し、
Websiteを作動させてみました。
基本的に動きは悪くないのですが、
http://127.0.0.1:5000/logout
をした時だけ以下のエラーが起きました。
発生している問題・エラーメッセージ
KeyError KeyError: 'logged_in' Traceback (most recent call last) File "C:\Users[]\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2328, in __call__ return self.wsgi_app(environ, start_response) File "C:\Users[]\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2314, in wsgi_app response = self.handle_exception(e) File "C:\Users[]\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1760, in handle_exception reraise(exc_type, exc_value, tb) File "C:\Users[]\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\_compat.py", line 36, in reraise raise value File "C:\Users[]\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2311, in wsgi_app response = self.full_dispatch_request() File "C:\Users[]\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1834, in full_dispatch_request rv = self.handle_user_exception(e) File "C:\Users[]\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1737, in handle_user_exception reraise(exc_type, exc_value, tb) File "C:\Users[]\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\_compat.py", line 36, in reraise raise value File "C:\Users[]\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1832, in full_dispatch_request rv = self.dispatch_request() File "C:\Users[]\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1818, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "G:\マイドライブ\for python\for website\simple_web.py", line 33, in do_logout session.pop('logged_in') File "C:\Users[]\AppData\Local\Programs\Python\Python37-32\lib\site-packages\werkzeug\datastructures.py", line 269, in pop rv = super(UpdateDictMixin, self).pop(key) KeyError: 'logged_in'
該当のソースコード
Python
1from flask import Flask, session 2 3from checker import check_logged_in 4 5app = Flask(__name__) 6 7@app.route('/') 8def hello() -> str: 9 return 'Hello from the simple web application.' 10 11@app.route('/page1') 12@check_logged_in 13def page1() -> str: 14 return 'This is page1' 15 16@app.route('/page2') 17@check_logged_in 18def page2() -> str: 19 return 'This is page 2.' 20 21@app.route('/page3') 22@check_logged_in 23def page3() -> str: 24 return 'This is page 3.' 25 26@app.route('/login') 27def do_login() -> str: 28 session['logged_in'] = True 29 return 'You are in now.' 30 31@app.route('/logout') 32def do_logout() -> str: 33 session.pop('logged_in') 34 return 'Currently out' 35 36app.secret_key = 'YouWillNeverGuessMySecretKey' 37 38if __name__ == '__main__': 39 app.run(debug=True)
何卒宜しくお願い致します。
loginページに行った後にlogoutページに行っても同じエラーが出ますか?