Flask + Wtforms : Not a valid choice
Bonjour,
Je débute en python et je profite d'un besoin pour coder une webappli avec Flask et Wforms.
Cependant je butte sur une erreur que je ne parviens pas à comprendre :
Citation:
{'menuDeroulantMois': ['Not a valid choice']}
Je ne comprends pas cette erreur.
Voici le code du formulaire :
Code:
1 2 3 4 5 6 7 8 9 10 11
| from flask_wtf import FlaskForm
from wtforms import StringField, TextField, SubmitField, SelectField
from wtforms.validators import DataRequired, Length
class ContactForm(FlaskForm):
"""Contact form."""
rechercheMois=[(1, "janvier"), (2, "fevrier"),(8, "aout"), (10, "octobre")]
#rechercheMois = [1,2,3,4,5,6,7,8,9,10,11,12]
menuDeroulantMois = SelectField(choices=rechercheMois,coerce=str)
submit = SubmitField('Valider') |
Ma fonction "route":
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| @app.route('/graphique/',methods=['GET','POST'])
def graphique():
maRecherche = 'toto'
a = 'rien'
form = ContactForm(coerce=str)
if form.validate_on_submit():
print(FlaskForm.errors)
# Connexion à la base de données
conn = sqlite3.connect(base)
curseur = conn.cursor()
connexion = sqlite3.connect(base)
curseur = connexion.cursor()
rechercheMois= form.menuDeroulantMois.data
#print(type(rechercheMois))
# --- Ma Requête ----
a = curseur.execute("select * from OPERATION where DateOperation > 01/?/2019", rechercheMois)
#a=curseur.execute("Select MontantOperation from OPERATION")
#a = a.fetchone()
a = a.fetchall()
print(a, "données :",form.menuDeroulantMois)
print(rechercheMois)
curseur.close()
connexion.close()
else:
print("ERREUR",form.errors)
print(form.menuDeroulantMois.data)
print(type(form.menuDeroulantMois.data))
return render_template('graphique.html', form=form, maRecherche=a) |
Le formulaire HTML:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| {# app/templates/upload.html #}
{% extends 'base.html' %}
{% block content %}
<h1>{% block title %} webappli - UploadFichier {% endblock %}</h1>
Faire une recherche dans les opérations bancaires.
<h1>RECHERCHE</h1>
<form action="" method="post">
{{ form.csrf_token }}
{% if form.errors %}
{{ form.errors }}
{% endif %}
<br> <br>
<u> {{ form.menuDeroulantMois.label }}:</u> {{ form.menuDeroulantMois }}<br>
</p>
<p>{{ form.submit() }}</p>
</form>
{{maRecherche}}
{% endblock content %} |