templates
index.html
todo.html
views.py
todo関数の中のif文はなんのためにあるのでしょうか?
試しに無くしてみても変わらないし、そもそもlogin関数の時に明らかにusernameをkeyに指定してからtodoを実行してるのにusernameがないことなどありましょうか??
python
1#main.py 2from flask import Flask, render_template, session, redirect, url_for, request 3app = Flask(__name__) 4app.secret_key = 'hXDm8NXqqJATH&7XHW6AtM.XEqM4cEMn' 5 6 7@app.route("/") 8def index(): 9 return render_template('index.html') 10 11@app.route("/login") 12def login(): 13 your_name = request.args.get("name", "") 14 if your_name: # これで入力がされたのかを確認してるんだと思う。 15 session['username'] = your_name 16 return redirect(url_for("todo")) 17 else: # 入力されてなかったら、最初の画面に戻る。 18 return render_template("index.html") 19 20@app.route("/logout") 21def logout(): 22 session.clear() 23 return render_template("index.html") 24 25@app.route("/todo") 26def todo(): 27 if 'username' not in session: #このif文はなんのためにあるんだろう。 28 return render_template("index.html") 29 return render_template("todo.html") 30 31
html
1<!-- index.html --> 2<!DOCTYPE html> 3<html> 4<head> 5<meta charset="utf-8"> 6<title>お名前を入力して下さい</title> 7</head> 8<body> 9 ログインしてください<br> 10 <form method="get" action="{{ url_for('login') }}"> 11 <p>名前:<input type="text" name="name" size="40"></p> 12 <button type="submit">ログイン</button> 13 </form> 14</body> 15</html>
html
1<!-- todo.html --> 2<!DOCTYPE html> 3<html> 4<head> 5<meta charset="utf-8"> 6<title>TODOリスト</title> 7</head> 8<body> 9 <h2>ようこそ{{ session['username'] }}さん</h2> 10 11 <br><a href="{{ url_for('logout')}}">ログアウト</a> 12</body> 13</html> 14
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/09 10:26