Flask validate_on_submit()始终返回false,但是当我删除它时,数据将添加到数据库中

时间:2018-12-29 16:47:49

标签: python flask-wtforms

我正在尝试向数据库中添加一个新学生,但是每次我单击注册时,页面都会重新加载,并且得到200的响应,但是没有任何内容添加到数据库中。我没有看到任何错误消息。 仅当删除if validate_on_submit()行时,我才能添加一个新的学生(和用户)。

我尝试了在Stack Overflow和其他地方可以找到的所有内容。 T

这是我的route.py文件:

@app.route('/admin/estudiantes', methods=['GET', 'POST'])

def estudiantes():

students = Student.query.all()
form = RegistrationForm()

print(form.errors)
if form.validate_on_submit():
    user = User(username=form.username.data, correo=form.email.data)
    estudiante = Student(nombre=form.nombre.data, apellido=form.apellido.data, direccion=form.direccion.data, telefono=form.telefono.data, f_nacmto=form.f_nacmto.data, Estudiante=user)
    user.set_password(form.password.data)

    db.session.add(estudiante)
    db.session.add(user)
    db.session.commit()
    flash('Felicidades, estas registrado')
    return redirect(url_for('login'))
return render_template('adm_student.html', title='LingoLand', students=students, form=form)




class RegistrationForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired()])
    email = StringField('Email', validators=[DataRequired()])
    password = PasswordField('Password', validators=[DataRequired()])
    password2 = PasswordField('Repetir Contraseña', validators=[DataRequired(), EqualTo('password')])
    submit = SubmitField('Registrarse')
    nombre = StringField('Name', validators=[DataRequired()])
    apellido = StringField('Apellido', validators=[DataRequired()])

    f_nacmto = DateField('Fecha de Nacimiento', format='%Y-%m-%d', validators=[DataRequired()])
    direccion = StringField('Direccion', validators=[DataRequired()])
    telefono = StringField('Telefono', validators=[DataRequired()])


def validate_username(self, username):
    user = User.query.filter_by(username=username.data).first()
    if user is not None:
        raise ValidationError('Favor de usar un nombre de usuario diferente.')

def validate_email(self, email):
    user = User.query.filter_by(correo=email.data).first()
    if user is not None:
        raise ValidationError('Este correro electronico ya existe.')

HTML:

<div class="card">
                <div class="card-body">
                  <h4 class="card-title">Formulario de Registracion rapida</h4>
              {% with messages = get_flashed_messages() %}
              {% if messages %}
                <ul>
                    {% for message in messages %}
                    <li>{{ message }}</li>
                    {% endfor %}
                </ul>
                {% endif %}
                {% endwith %}

              <form class="forms-sample" id="resetform" action="" method="POST" novalidate>
                {{ form.hidden_tag() }}
                <div class="form-group">

                  <label for="Nombre">Nombre de Usuario</label>

                  {{ form.username(class_="form-control", placeholder="usuario") }}

                  {% for error in form.username.errors %}
                        <span style="color: red;">[{{ error }}]</span>
                    {% endfor %}
                </div>

                <div class="form-group">

                  <label for="email">Correo</label>

                  {{ form.email(class_="form-control", placeholder="email") }}

                  {% for error in form.email.errors %}
                        <span style="color: red;">[{{ error }}]</span>
                    {% endfor %}
                </div>


                <div class="form-group">

                  <label for="password">Contraseña</label>

                  {{ form.password(class_="form-control", placeholder="contraseña") }}

                  {% for error in form.password.errors %}
                        <span style="color: red;">[{{ error }}]</span>
                    {% endfor %}
                </div>



                <div class="form-group">


                  {{ form.nombre(class_="form-control", placeholder="Nombre") }}

                  {% for error in form.nombre.errors %}
                        <span style="color: red;">[{{ error }}]</span>
                    {% endfor %}
                    <label for="nombre">Nombre</label>
                </div>

                <div class="form-group">

                  <label for="apellido">Apellido</label>

                  {{ form.apellido(class_="form-control", placeholder="Apellido") }}

                  {% for error in form.apellido.errors %}
                        <span style="color: red;">[{{ error }}]</span>
                    {% endfor %}
                </div>

                <div class="form-group">

                  <label for="f_nacmto">Fecha de Nacimiento</label>

                  {{ form.f_nacmto(class_="form-control", placeholder="Fecha nacimiento") }}

                  {% for error in form.f_nacmto.errors %}
                        <span style="color: red;">[{{ error }}]</span>
                  {% endfor %}
                </div>

                <div class="form-group">

                  <label for="f_nacmto">Telefono</label>

                  {{ form.telefono(class_="form-control", placeholder="Telefono") }}

                  {% for error in form.telefono.errors %}
                        <span style="color: red;">[{{ error }}]</span>
                  {% endfor %}
                </div>

                <div class="form-group">

                  <label for="f_nacmto">Direccion</label>

                  {{ form.direccion(class_="form-control", placeholder="Direccion") }}

                  {% for error in form.direccion.errors %}
                        <span style="color: red;">[{{ error }}]</span>
                  {% endfor %}
                </div>








                {{ form.submit(class_="btn btn-success mr-2") }}
                <button class="btn btn-light" id="clearform">Cancelar</button>
              </form>
            </div>
          </div>

0 个答案:

没有答案