python - Flask Python 提交按钮

标签 python html twitter-bootstrap flask

我正在尝试创建一个页面来注册用户,但 Bootstrap 表单中的提交按钮不起作用。当我点击提交按钮时,我收到了错误的请求错误。这是我的 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')

我还有一个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 %}

我找不到哪里做错了,我是Python和Flask的新手!

最佳答案

您的输入字段没有name属性。这将导致您的所有检查都导致 KeyError。第一步是将属性添加到每个输入。

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

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

下一步将更改检查字段的方式。如果您只关心字段的存在,那么 in 就是正确的选择。

if 'username' not in request.form:

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

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

关于python - Flask Python 提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27410912/

相关文章:

jquery - Bootstrap Accordion 菜单不起作用

twitter-bootstrap - 如何在 Eclipse 中为 jsp 页面添加 Bootstrap

html - Internet Explorer 中的 Segoe UI 字体

html - 绝对定位元素的性能

css - Sass 在 "content"CSS 属性中的 unicode 字符后添加了额外的空格

Python:在字符串上使用拆分并返回元组?

css - 无法让父 div 与子 div 3 childs 跨越

python - 如何使用 Python 从网络浏览器获取 cookie?

Python 在使用 __repr__ __str__ 时打印内存地址而不是列表?

java - 如何为 Java HashMaps 模拟 Python dict "items()"方法?