简体   繁体   English

WTForms /烧瓶形式无法通过提交按钮正确检索数据

[英]WTForms/flaskforms not retrieving data properly via submit button

So I'm just trying to get the submit button to work properly. 所以我只是想让“提交”按钮正常工作。 Work properly meaning to get the user input for email and password to be directed to my login. 正常工作意味着获取用户输入的电子邮件和密码以直接登录到我的登录名。

Currently, it only redirects to the index.html, but I want it to go result with a redirect to either profile or error. 当前,它仅重定向到index.html,但我希望它以重定向到配置文件或错误的结果来实现。

Here's the python part: 这是python部分:

@app.route("/login", methods=["GET", "POST"]) @ app.route(“ / login”,Methods = [“ GET”,“ POST”])

def login(): def login():

"""Log user in if credentials provided are correct."""

form = LoginForm(request.post)

# this is if its POST
if form.validate and request.method == 'POST':
    email = request.form['email']
    password = request.form['password']

    if email == admin@gmail.com" and password == "admin":
        return redirect(url_for('/home'))
    else:
        return redirect(url_for('/error'))
# this is if its GET
#return render_template("index.html", form=form)

This is the login form 这是登录表格

class LoginForm(FlaskForm): 类LoginForm(FlaskForm):

email = StringField('email', validators=[InputRequired()])
password = PasswordField('password', validators=[InputRequired()])
remember = BooleanField('remember me')

Here's the html part: 这是html部分:

<div class="modal-body">
                <form role="form">
                    <div class="form-group">
                        <div class="input-group">

                            <form method="POST" action="{{ url_for('login')}}">
                                {{ form.csrf }}
                               <dl style="width: 100%;">
                                    <div class="form-group">
                                        <form role="form">
                                            <div class="form-group">
                                                <div class="input-group">
                                                    {{ wtf.form_field(form.email) }}
                                                    {{ wtf.form_field(form.password) }}
                                                </div>
                                            </div> <!-- /.form-group -->
                                        </form>
                                    </div> <!-- /.form-group -->

                                    <div style="margin-left: 70%;" class="checkbox">
                                        {{ wtf.form_field(form.remember) }}
                                    </div>

                                    <div class="modal-footer">
                                       <input class="btn btn-lg btn-primary btn-block" style="background-color: #3eb2a0;border-color: #3eb2a0;" type="submit" value="Sign In">
                                    </div>
                                </dl>
                            </form>

                        </div>
                    </div>
                </form>
            </div>

Only problems i see with your code is one: 我发现您的代码只有一个问题:

if email == admin@gmail.com" and password == "admin":
    return redirect(url_for('/home'))

You do not have a quotation mark before admin@gmail.com 您在admin@gmail.com之前没有引号

two: 二:

return redirect(url_for('/home'))

is there a reason for the forward slash? 有正斜线的原因吗? have you tried 'home' 你试过“回家”了吗

edit: 编辑:

Here is an example of how i set up views that are similar to yours 这是我如何设置与您相似的视图的示例

@bp.route('/upvote', methods=['GET', 'POST'])
def view():

form = key()


if form.validate_on_submit():
    received_key = form.key_code.data
    url = form.url.data
    username = form.username.data

    return redirect(url_for('views.success')) #views is the name of the blueprint the success route is in


return render_template('upvote.html', title='title here', form=form)

form.validate_on_submit(): Takes the place of form.validate and form.submit.data/if response.method ='POST' form.validate_on_submit():代替form.validate和form.submit.data/if response.method ='POST'

and you can then retrieve the data stored in the forms by form.variable.data. 然后您可以通过form.variable.data检索存储在表单中的数据。

Check to see if you are even receiving data back from the forms at all. 检查以查看您是否甚至还没有收到来自表单的数据。 It seems like it might not be recieving the post request and skipping everything under your "if request.method = 'POST'" statement. 似乎可能无法接收到发布请求并跳过“ if request.method ='POST'”语句下的所有内容。

Your view would look like: 您的视图如下所示:

@app.route("/login", methods=["GET", "POST"])

def login()
form = LoginForm()

# this is if its POST
if form.validate_on_submit():
    email = form.email.data
    password = form.password.data

    if email == "admin@gmail.com" and password == "admin":
        return redirect(url_for('home'))
    else:
        return redirect(url_for('error'))


#return render_template("index.html", form=form)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM