我尝试在Flask中设置cookie,但是我没有得到想要的东西。我没有获得用户名,而是在URL上附加了响应。 我的 routes.py
@app.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('index'))
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user is None or not user.check_password(form.password.data):
flash('Invalid username or password')
return redirect(url_for('login'))
login_user(user, remember=form.remember_me.data)
userCookie = request.form['username']
resp = make_response(render_template('index.html'))
resp.set_cookie('user', userCookie)
next_page = request.args.get('next')
if not next_page or url_parse(next_page).netloc != '':
next_page = url_for('index', resp=resp)
return redirect(next_page)
return render_template('login.html', title='Sign In', form=form)
我想在 index.html
中显示Cooki的内容{% for r in resp %}
{{ r }}
{% endfor %}
相反,我得到:
index?resp<Response+1250+bytes+[200+OK]>
我在做什么错了?
[编辑-注销方法]
这是我添加Cookie之前的方法
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('index'))
因此,如果我添加了Cookie:
@app.route('/logout')
def logout():
resp = make_response(redirect('/login'))
resp.delete_cookie('user')
如果我重新启动服务器,登录,就创建了cookie,但是注销后,我什至可以转到端点/ login 返回resp
答案 0 :(得分:1)
您需要直接返回resp,make_response
将返回值从视图函数转换为response_class的实例。
userCookie = request.form['username']
resp = make_response(render_template('index.html'))
resp.set_cookie('user', userCookie)
next_page = request.args.get('next')
if not next_page or url_parse(next_page).netloc != '':
return resp
在html中,通过javascript获取cookie
<label id="label_id1"></label>
<script type="text/javascript">
alert(document.cookie);
document.getElementById('label_id1').innerhtml = document.cookie
</script>
答案 1 :(得分:1)
在一个请求中设置了cookie,并且可以在另一个请求中使用它。
要解决此问题,请在redirect
中使用make_response
。
我附上了使用cookie进行登录/注销功能的示例:
app.py
:
from flask import Flask, render_template, request, make_response, flash, redirect
app = Flask(__name__)
app.config['SECRET_KEY'] = 'SUPER SECRET'
@app.route('/', methods = ['GET'])
def home():
username = request.cookies.get('username')
if username:
return render_template('home.html', username=username)
return render_template('home.html')
@app.route('/login', methods = ['GET','POST'])
def login():
username = request.cookies.get('username')
if username:
return render_template('login.html', username=username)
if request.method=='POST':
username = request.form.get('username')
password = request.form.get('password')
if username=='admin' and password=='admin':
flash("Successful login", "success")
resp = make_response(redirect('/'))
resp.set_cookie('username', username)
return resp
else:
flash("Wrong username or password", "danger")
return render_template('login.html')
@app.route('/logout', methods = ['GET'])
def logout():
resp = make_response(redirect('/'))
resp.delete_cookie('username')
return resp
app.run(debug=True)
home.html
:
<html>
<head>
<title>Home</title>
</head>
<body>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul class=flashes>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
{% if username %}
Welcome {{ username }}.
<a href="{{ url_for('logout') }}">Click here</a> to logout.
{% else %}
You are not logged in.
<a href="{{ url_for('login') }}">Click here</a> to login.
{% endif %}
</body>
</html>
login.html
:
<html>
<head>
<title>Login</title>
</head>
<body>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul class=flashes>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
{% if username %}
You are already logged in as{{ username }}.
<a href="{{ url_for('home') }}">Click here</a> to go to home.
<a href="{{ url_for('logout') }}">Click here</a> to logout.
{% else %}
<form method="post" action="">
<label for="username">Username</label>
<input type="text" name="username" id="username"/>
<br/>
<label for="password">Password</label>
<input type="password" name="password" id="password"/>
<br/>
<input type="submit" name="submit" id="submit" value="Login"/>
</form>
{% endif %}
</body>
</html>
屏幕截图:
1。登录之前(无cookie):
2。登录(无cookie):
3。登录后(收到Cookie):
4。注销后(无cookie):