sessionの利点として、ログイン状態の維持があるというのがわかったので、ファイルがとんでもsessionの値を連続で使えるか試してみたんですけど、なんだかうまくいきません。どこが間違ってるんでしょうか??
html
1<!DOCTYPE html> 2<html> 3<head> 4<meta charset="UTF-8"> 5<title>ページ1</title> 6</head> 7<body> 8 <h1>sessionでログイン状態を維持できるのを試してみる</h1> 9 <form method="get" action="{{"process1"}}"> 10 <p>名前:<input type="text" name="name" size="40"></p> 11 <button>送信!</button> 12 </form> 13</body> 14</html> 15
html
1<!DOCTYPE html> 2<html> 3<head> 4<meta charset="UTF-8"> 5<title>ページ2</title> 6</head> 7<body> 8 <h1>ようこそ{{ session['username'] }}さん</h1> 9 <form method="get" action="{{ "process3" }}"> 10 <button type="submit">次の画面へ!</button> 11 </form> 12 13</body> 14</html> 15
html
1<!DOCTYPE html> 2<html> 3<head> 4<meta charset="utf-8"> 5<title>ページ3</title> 6</head> 7<body> 8 <h1>{{ session['username']}}さん、今日も天気がいいですね。</h1> 9 <form method="get" action="{{"process4"}}"> 10 <button type="submit">送信!</button> 11 </form> 12 13</body> 14</html>
html
1<!DOCTYPE html> 2<html> 3<head> 4<meta charset="utf-8"> 5<title>ページ4</title> 6</head> 7<body> 8 <h1>{{ session['username']}} さんお疲れサマです。</h1> 9</body> 10</html>
python
1from flask import Flask, render_template, request, redirect, url_for 2app = Flask(__name__) 3 4@app.route("/") 5def index(): 6 return render_template("index.html") 7 8@app.route("/process1") #ここはusernameを受け取るの専用なのかね。 9def process1(): 10 your_name = request.args.get("name", "") 11 if your_name: 12 session['username'] = your_name 13 return redirect(url_for("process2")) 14 return redirect(url_for("index")) 15 16@app.route("/process2") 17def process2(): 18 return render_template("index2.html") 19 20@app.route("/process3") 21def process3(): 22 return render_template("input.html") 23 24@app.route("/process4") 25def process4(): 26 return render_template("output.html") 27 28
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/11 13:07