尝试输入内部表单输入时如何停止自动刷新?

时间:2017-08-29 07:05:04

标签: python html5 flask flask-wtforms

我正在使用Flask框架编写一个python Web应用程序,并使用postgreSQL db编写Flask-WTF,Flask-Login扩展。 每当我尝试在输入字段中输入电子邮件或密码时,页面都会刷新,我无法输入任何内容。如何解决此问题?似乎有多个GET请求被发送到我的视图功能。

HTML:

<!-- BODY OF THE LOGIN FORM -->
    <div class = "login">
        <form id = "loginform" action = "{{ url_for('login') }}" method = "post">
            {% from "formhelpers.html" import render_field %}
            {{render_field(form.email)}}
            {{render_field(form.password)}}
            <input type = "submit" class = "btn btn-default" value = "Log In">  
        </form>
    </div>

PYTHON / FLASK查看功能:

class LoginForm(FlaskForm):
    email = StringField('Email', validators=[DataRequired()])
    password = PasswordField('Password', validators=[DataRequired()])
    submit = SubmitField('Send')

@app.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm()
    if request.method == 'POST' and form.validate_on_submit():
        #retrieve data from user
        checkemail = form.email.data
        checkpassword = form.password.data
        #query database for user email 
        founduser = User.query.filter_by(email=checkemail).first()
        #check password hashes if user is found by email
        if founduser:
            if hashpw(checkpassword, founduser.password.encode('utf-8')) == founduser.password:
                return "SUCCESS: Logged In"
            #incorrect password
            else:
                return "ERROR: Invalid Details"
        #incorrect email address
        else:
            return "ERROR: User Doesn't Exist"
    #passing an instance of our login form class to HTML for rendering
    else:
        return render_template('login.html', form=form)

这是我的终端窗口的屏幕截图。当我尝试输入电子邮件或密码字段时,我收到了很多GET请求。 screenshot terminal window

1 个答案:

答案 0 :(得分:0)

您应该使用ajax向您提交表格 我没有使用过flask,但是ajax在html中是类似的,你可以使用jquery发布你的ajax请求