twitter-bootstrap Flask Python 提交按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27410912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 21:59:36  来源:igfitidea点击:

Flask Python submit button

pythonhtmltwitter-bootstrapflask

提问by alpi

I am trying to create a page to register users but the submit button in my bootstrap form isn't working. When I hit the submit button, I get a bad request error. Here is the code in my python file:

我正在尝试创建一个页面来注册用户,但我的引导程序表单中的提交按钮不起作用。当我点击提交按钮时,我收到一个错误的请求错误。这是我的python文件中的代码:

@app.route('/register', methods=['GET', 'POST'])
def register():
    if request.method == 'POST':
        if not request.form['username']:
            error = 'You have to enter a username'
        elif not request.form['email'] or '@' not in request.form['email']:
            error = 'You have to enter a valid email address'
        elif not request.form['password']:
            error = 'You have to enter a password'
        elif get_user_id(request.form['username']) is not None:
            error = 'The username is already taken'
        else:
            print(request.form['username'])
            db = get_db()
            db.execute('INSERT INTO user (username, email, pw_hash) VALUES (?, ?, ?)',
                       [request.form['username'], request.form['email'],
                        generate_password_hash(request.form['password'])])
            db.commit()
            flash('You were successfully registered and can login now')
            return render_template('control.html')
    return render_template('register.html')

also i have a html file register.html:

我也有一个 html 文件 register.html:

{% extends 'layout.html' %}
{% block title %}Sign-up{% endblock title %}
{% block body %}
<div class="container">
    <form class="form-register" role="form" method="post" action="{{ url_for('register') }}">
        <h2 class="form-register-heading">Please sign up</h2>
        <label for="username" class="sr-only">Username</label>
        <input type="username" id="inputUsername" class="form-control" value="{{ request.form.username }}" placeholder="Username" required autofocus>
        <label for="email" class="sr-only">Email address</label>
        <input type="email" id="inputEmail" class="form-control" value="{{ request.form.email }}" placeholder="Email address" required autofocus>
        <label for="password" class="sr-only">Password</label>
        <input type="password" id="inputPassword" class="form-control" placeholder="Password" required >
        <button class="btn btn-lg btn-primary btn-block" type="submit">Sign up</button>
    </form>

</div>
{% endblock body %}

I can't find where I did it wrong, I'm new to python and flask!

我找不到我做错的地方,我是 python 和烧瓶的新手!

采纳答案by dirn

Your inputfields have no nameattribute. This will cause all of your checks to result in KeyErrors. The first step is to add the attribute to each input.

您的input字段没有name属性。这将导致您的所有检查结果为KeyErrors。第一步是为每个输入添加属性。

<input name="username" type="text" id="inputUsername" class="form-control" value="{{ request.form.username }}" placeholder="Username" required autofocus>

Note that I also checked the typeattribute as there is no usernametype. emailand passwordare valid values, emailbeing added in HTML5.

请注意,我还检查了type属性,因为没有username类型。emailpassword是有效值,email在 HTML5 中添加。

The next step will be to change how you check for the fields. If you only care about the presence of the field, inis the way to go.

下一步将是更改检查字段的方式。如果你只关心场上的存在,in是要走的路。

if 'username' not in request.form:

If, however, you also want a truty value, the getmethod is what you want.

但是,如果您还想要一个真实值,那么该get方法就是您想要的。

if not request.form.get('username'):