First off, I'm new to python and Flask, so I'm sorry if my question is dumb. I am looking for it but have not found an answer (which should be "easy", I think).
I wanted to add a contact page on my website, I found this tutorial , so I followed it. Everything worked fine until the form was confirmed. I use only Required , and form.validate () always returns false. If I donβt touch my code, and I delete every Required in the form class, it works fine, form.validate () returns true.
I really donβt understand why, I read a lot that you need to use validate_on_submit () , but I get an error if I use it: * The ClassName object does not have the attribute 'validate_on_submit' *
Here are the relevant parts of the code:
Index.py
@app.route('/contact', methods=['GET','POST']) def contact(): form = ContactForm() if request.method == 'POST': if form.validate() == False: flash('All Fields are required.') return render_template('contact.html', form=form) else: return 'Form posted' elif request.method == 'GET': return render_template('contact.html', form=form)
forms.py
from wtforms import Form, TextField, TextAreaField, SubmitField, validators,ValidationError class ContactForm(Form): name = TextField("Name", [validators.Required()]) email = TextField("Email") subject = TextField("Subject") message = TextAreaField("Message") submit = SubmitField("Send")
contact.html
<div id="contact"> {% for message in get_flashed_messages() %} <div class="flash">{{ message }}</div> {% endfor %} <form action="{{ url_for('contact') }}" method=post> {{ form.name.label }} {{ form.name }} {{ form.email.label }} {{ form.email }} {{ form.subject.label }} {{ form.subject }} {{ form.message.label }} {{ form.message }} {{ form.submit }} </form> </div>
I never got the line βForm Submittedβ even when I write something in the βNameβ field.
Thanks in advance,