【发布时间】:2018-07-30 01:35:38
【问题描述】:
在测试此代码时,我收到错误“400 Bad Request: KeyError: 'username'”,我不知道为什么
这是代码,我正在使用烧瓶来做这个
@app.route('/')
def index():
if 'username' in session:
username = session['username']
return 'Logged in as ' + username + '<br>' + \
"<b><a href = '/logout'>click here to log out</a></b>"
return "You are not logged in <br><a href = '/login'></b>" + \
"click here to log in</b></a>"
@app.route('/login', methods = ['GET', 'POST'])
def login():
if request.method == 'POST':
session['username'] = request.form['username']
return redirect(url_for('index'))
return '''
<form action = "" method = "post">
<p><input type = text name = username/></p>
<p><input type = submit value = Login /></p>
</form>
'''
@app.route('/logout')
def logout():
#remove the session from username if it is there
session.pop('username', None)
return redirect(url_for('index'))
【问题讨论】:
-
为什么不在 html 代码中的值(如“用户名”)周围添加任何
"?