简体   繁体   中英

Flask Form not submitting

I am learning Flask and trying build a very simple app which has few parts creating new users and searching the existing users. The create/register new users part is working just fine, but for some reason the search function is not working as expected because the form is not getting submitted. I have tried to look every possible case which I think of, searched Stackoverflow as well but could not get to fix it.

The code for registration functionality:

forms.py

from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired, length

class Register(FlaskForm):
    name = StringField('name', validators=[DataRequired(), length(min=1, max=30)])
    email = StringField('email', validators=[DataRequired()])
    submit = SubmitField('Register')

class UserSeacrh(FlaskForm):
    name = StringField('name', validators=[DataRequired()])
    email = StringField('email', validators=[DataRequired()])
    submit = SubmitField('Search')

views.py

@app.route("/register", methods=['GET', 'POST'])
def register():
    form = Register()
    if form.validate_on_submit():
        db.create_all()
        user = CreateUser(form.name.data, form.email.data)
        db.session.add(user)
        db.session.commit()
        flash('User {} created successfully'.format(form.name.data))
        return redirect(url_for("register"))
    return render_template("register.html", form=form, title='New User Registration')


@app.route("/search", methods=['GET', 'POST'])
def user_profile():
    form = UserSeacrh()
    if form.validate_on_submit():
        flash('{}'.format(user))
        return redirect(url_for("home"))
        #return render_template("users.html", users=user)
    return render_template("userSearch.html", form=form)

register.html

<!DOCTYPE html>

{% extends "base.html" %}

{% block body %}
    <form action="" method="post">
         {{ form.hidden_tag() }}
        <div>
            <h4>User Registeration</h4>
            <span>Name {{form.name}}</span>
             <span style="color: red">
                {% for error in form.name.errors %}
                    {{error}}
                {% endfor %}
            </span
            <span>Email {{form.email}}</span>
            <span style="color: red">
                {% for error in form.email.errors %}
                    {{error}}
                {% endfor %}
            </span>

        </div>
        {{form.submit()}}
    </form>

    {% with messages = get_flashed_messages() %}
        {% if messages %}
        <br/>
        <ul>
            {% for message in messages %}
                <li>{{message}}</li>
            {% endfor %}
        </ul>
        {% endif %}
    {% endwith %}
{% endblock %}

userSearch.html

<!DOCTYPE html>

{% extends "base.html" %}

{% block body %}

    <form action="" method="post">
         {{ form.hidden_tag() }}
         <h4>User Search Form</h4>
        <div>
            <span>Email {{form.email}}</span>

            <span style="color: red">
                {% for error in form.email.errors %}
                    {{error}}
                {% endfor %}
            </span>
        </div>
        {{form.submit()}}
    </form>

    {% with messages = get_flashed_messages() %}
        {% if messages %}
        <br/>
        <ul>
            {% for message in messages %}
                <li>{{message}}</li>
            {% endfor %}
        </ul>
        {% endif %}
    {% endwith %}

{% endblock %}

I am clueless now, can someone please help me out here?

Most likely the reason is that in the userSearch.html you didn't add the name field. In the UserSeacrh class the field name has a validator DataRequired which most likely creates the problem. Just remove the name field in the UserSeacrh class if you want to do the search by email only.

By the way there is a typo in UserSeacrh - it should be UserSearch . But in your case it's not the problem.

我认为您需要为表单指定操作,即将完成的表单POST到哪个URL。

<form action="" method="post">

通过从forms.py中删除UserSearch表单中的名称字段,我能够使用Nurzhan'提示解决这个问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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