请求不通过

时间:2016-09-28 03:10:07

标签: flask flask-wtforms

我创建一个登录页面,代码即时发布不会给出任何错误

if request.method == 'POST' and formLogin.validate():
        if formLogin.username_signin == 'admin' and formLogin.password_signin == 'admin':
            return redirect(url_for('home')) 

这段代码被跳过了,即使我把管理员放在盒子里(wtforms)这里的代码我正在使用:

app = Flask(__name__)
app.secret_key = 'jjjjjjj'

@app.route('/')
def home():
    return render_template('home.html')


@app.route('/login/', methods=['GET', 'POST'])
def login():


    formLogin = LoginForm(request.form)
    error = None

    if request.method == 'POST' and formLogin.validate():
        if formLogin.username_signin == 'admin' and formLogin.password_signin == 'admin':
            return redirect(url_for('home'))
        else:
            error = 'Please enter right credentials.'
            return render_template('login.html', error=error, formLogin=formLogin)

    return render_template('login.html', error=error, formLogin=formLogin)


if __name__ == '__main__':
    app.debug = True
    app.run()

我的login.html:

{% extends 'base.html' %}

 {% block head %}
        <div class='container'>
            <div class='row'>
                <form method= post action='/login/'>
                    {{ formLogin.username_signin(class='form-control', placeholder='Username') }}
                    {{ formLogin.password_signin(class='form-control', placeholder='Password') }}
                    {{ formLogin.submit_signin(class='btn btn-primary') }}
                </form>
                <p><strong>Error:</strong>{{ error }}</p>
            </div>    
        </div>
        {% endblock %}

Form.py

from wtforms import Form,StringField, PasswordField, TextField, BooleanField, SubmitField, validators

class LoginForm(Form):  

    username_signin = StringField('SigninU', [validators.Required('please enter name')])
    password_signin = PasswordField('signinP', [validators.Required('need password')])
    submit_signin = SubmitField('Signin')

就像我说的一切都呈现正常而且我没有得到错误加载我的问题是当我在两个框中放入'admin'而不是将我重定向到home()函数时它只是直接跳到else语句。

1 个答案:

答案 0 :(得分:1)

您必须使用.data来访问get_data。

if request.method == 'POST' and formLogin.validate():
    if formLogin.username_signin.data == 'admin' and formLogin.password_signin.data == 'admin':
        return redirect(url_for('home'))