Rohit
Rohit

Reputation: 4188

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?

Upvotes: 5

Views: 10988

Answers (3)

Rohit
Rohit

Reputation: 4188

I was able to solve this using Nurzhan' tip by removing the name field from UserSearch form from forms.py .

Upvotes: 0

Nurjan
Nurjan

Reputation: 6073

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.

Upvotes: 6

Katie
Katie

Reputation: 51

I think you need to specify the action for your form, i.e., what URL to POST the completed form to.

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

Upvotes: 5

Related Questions