简单 request.form 导致错误处理程序而不是登录
simple request.form is causing errorhandler instead of signing in
当尝试使用相关的用户名和密码登录 index.html 页面时,错误处理程序被激活导致 404.html,而不是登录被接受或用户名和密码不正确信息。无论是否使用正确的登录,都会发生这种情况。
有什么关于我遗漏或做错的建议吗?
我应该使用 request.form.get 还是 request.form.post?
@app.errorhandler(404)
@app.errorhandler(500)
def errorpage(e):
return render_template('404.html')
def login_required(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return f(*args, **kwargs)
else:
flash('please login first.')
return redirect (url_for('index'))
return wrap
@app.route('/', methods=['GET','POST'])
def index():
error = None
if request.method == 'POST':
if request.form['username'] != 'admin' or request.form['password'] != 'password':
error = 'invalid attempt.'
else:
session['logged_in'] = True
return redirect(url_for('admin'))
return render_template('index.html', error = error)
@app.route('/logout')
def logout():
session.pop('logged_in', None)
return render_template('/logout.html')
@app.route('/admin',methods=['GET','POST'])
@login_required
def admin():
这里是关联的index.html
<!DOCTYPE html>
<html lang="en"
<header>
<link rel="stylesheet" type="text/css" href="static/bootstrap.min.css" media='screen'>
<title>Login</title>
<h1>Welcome!</h1>
<h2>Sign in</h2>
</header>
<body>
<form action="/helloflask.py" method="post">
Username: <input type="text" placeholder="Username" name="username" value="{{ request.form.username }}"><br>
Password: <input type="text" placeholder="Password" name="password" value="{{ request.form.password }}"><br><br><br>
<input class= "btn btn-default" type="submit" value="Confirm">
</form>
{% if error %}
<p class="error"><strong>Error:</strong> {{ error }} </p>
{% endif %}
</body>
</html>
form的action
属性应该是处理登录功能的端点。在这种情况下,您应该将 helloflask.py
替换为 /
。
当尝试使用相关的用户名和密码登录 index.html 页面时,错误处理程序被激活导致 404.html,而不是登录被接受或用户名和密码不正确信息。无论是否使用正确的登录,都会发生这种情况。
有什么关于我遗漏或做错的建议吗?
我应该使用 request.form.get 还是 request.form.post?
@app.errorhandler(404)
@app.errorhandler(500)
def errorpage(e):
return render_template('404.html')
def login_required(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return f(*args, **kwargs)
else:
flash('please login first.')
return redirect (url_for('index'))
return wrap
@app.route('/', methods=['GET','POST'])
def index():
error = None
if request.method == 'POST':
if request.form['username'] != 'admin' or request.form['password'] != 'password':
error = 'invalid attempt.'
else:
session['logged_in'] = True
return redirect(url_for('admin'))
return render_template('index.html', error = error)
@app.route('/logout')
def logout():
session.pop('logged_in', None)
return render_template('/logout.html')
@app.route('/admin',methods=['GET','POST'])
@login_required
def admin():
这里是关联的index.html
<!DOCTYPE html>
<html lang="en"
<header>
<link rel="stylesheet" type="text/css" href="static/bootstrap.min.css" media='screen'>
<title>Login</title>
<h1>Welcome!</h1>
<h2>Sign in</h2>
</header>
<body>
<form action="/helloflask.py" method="post">
Username: <input type="text" placeholder="Username" name="username" value="{{ request.form.username }}"><br>
Password: <input type="text" placeholder="Password" name="password" value="{{ request.form.password }}"><br><br><br>
<input class= "btn btn-default" type="submit" value="Confirm">
</form>
{% if error %}
<p class="error"><strong>Error:</strong> {{ error }} </p>
{% endif %}
</body>
</html>
form的action
属性应该是处理登录功能的端点。在这种情况下,您应该将 helloflask.py
替换为 /
。