无法将文本输入表格(Flask,WTForm)

时间:2019-06-09 16:48:17

标签: python html flask flask-wtforms wtforms

我想用Flask和WTForms建立一个网站。但是我无法使文本输入起作用(该页面不允许我在框中输入任何文本)。你能告诉我怎么了吗?

页面的外观如下:

enter image description here

这是我使用WTForms创建的表单

from flask_wtf import FlaskForm
from wtforms import SubmitField, IntegerField
from wtforms.validators import DataRequired, NumberRange, EqualTo


class RequestDataForm(FlaskForm):

    feature_count = IntegerField('Number of features', validators=[DataRequired(), NumberRange(min=1, max=50)])

    effective_rank = IntegerField('Effective Rank', validators=[DataRequired(), NumberRange(min=1, max=EqualTo(feature_count))])

    noise = IntegerField('Noise', validators=[DataRequired(), NumberRange(min=0, max=1)])

    submit = SubmitField('Submit') 

这是layout.html

<!DOCTYPE html>
<html>
<style>
</style>
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
    <main role="main" class="container">
      <div class="row">
        <div class="col-md-8">
          {% with messages = get_flashed_messages(with_categories=true) %}
            {% if messages %}
              {% for category, message in messages %}
                <div class="alert alert-{{ category }}">
                  {{ message }}
                </div>
              {% endfor %}
            {% endif %}
          {% endwith %}
          {% block page_content %}{% endblock %}
        </div>
      </div>
    </main>
</body>
</html>

这是我的page.html

{% extends 'layout.html' %}
{% block page_content %}
    <div class="content-section">
        <form method="POST" action="">
            {{ form.hidden_tag() }}
            <fieldset class="form-group">
                <legend class="border-bottom mb-4">Fill with values.</legend>
                <div class="form-group">
                    {{ form.feature_count.label(class="form-control-label") }}
                    {{ form.feature_count.label(class="form-control form-control-lg") }}
                </div>
                <div class="form-group">
                    {{ form.effective_rank.label(class="form-control-label") }}
                    {{ form.effective_rank.label(class="form-control form-control-lg") }}
                </div>
                <div class="form-group">
                    {{ form.noise.label(class="form-control-label") }}
                    {{ form.noise.label(class="form-control form-control-lg") }}
                </div>
            </fieldset>
            <div class="form-group">
                {{ form.submit(class="btn btn-outline-info") }}
            </div>
        </form>
    </div>
{% endblock page_content%}

1 个答案:

答案 0 :(得分:2)

根据您的HTML,您只为每个表单元素而不是元素本身输出label。尝试将它们全部更新为:

<div class="form-group">
    {{ form.noise.label(class="form-control-label") }}
    {{ form.noise(class="form-control form-control-lg") }}
</div>