Eri*_*ick 6 python login flask
我正在尝试更多地了解Flask的项目,我想知道是否有人可以向我解释为什么示例代码列出方法'GET'和'POST',当它只尝试处理登录时如果请求是'POST'?
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if request.form['username'] != app.config['USERNAME']:
error = 'Invalid username'
elif request.form['password'] != app.config['PASSWORD']:
error = 'Invalid password'
else:
session['logged_in'] = True
flash('You were logged in')
return redirect(url_for('show_entries'))
# Note that nowhere do we seem to care about 'GET'...
return render_template('login.html', error=error)
Run Code Online (Sandbox Code Playgroud)
GET和POST方法都由您的函数处理.
使用GET时,将login.html返回登录表单()以供用户登录.这是该函数的最后一行.
使用POST时,使用提供的登录名/密码验证表单.之后,用户被重定向到另一个页面(url for show_entries),或者另一次发送登录表单时出现相关错误.
您应该阅读" 何时使用POST以及何时使用GET?'有关使用POST处理登录表单的原因以及使用GET进行发送的原因的详细信息.
return render_template('login.html', error=error) 是的处理程序GET.
想想逻辑:
render_template获得那些错误,否则它会None从方法的开头得到.我假设如果出现错误None,render_template它可能只是呈现一个普通的'登录表单.Note: I've never used flask, but I understand python
| 归档时间: |
|
| 查看次数: |
2316 次 |
| 最近记录: |