简单的request.form导致错误处理程序而不是登录

时间:2017-04-23 00:46:19

标签: python html flask flask-login

尝试使用相关的用户名和密码登录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>

1 个答案:

答案 0 :(得分:0)

表单的action属性应该是处理登录功能的端点。在这种情况下,您应该将helloflask.py替换为/