lundi 12 juin 2017

[Python][Jinja][WTFORMS] Looping through checkbox

I am building a form using :

  1. A selectfield
  2. A submitfield
  3. A list of dynamical checklist (year of today to year + 5) which will be later use as a filter.

I have a "forms.py" file which create the form with wtforms :

wtforms import Form, SelectField, SubmitField, BooleanField
from datetime import datetime


class BasicForm(Form):
    class ListOfCheck():
        pass
    active = SelectField(u'Division : \t ', choices=[('BNL', 'Benelux'), ('TG', 'Generation')])
    action = SubmitField(u'Get data')
    today = datetime.today().year
    checklist = ListOfCheck()
    for year in range(today, today + 5 ):
        check_name = "check" + str(year)
        check = BooleanField(str(year))
        setattr(ListOfCheck,check_name,check)

Then I use a template to display my form :

<!DOCTYPE html>
<html>
  <head>
    <title>MtM and MtR</title>
    <link href="http://ift.tt/2sTgWMc" rel="stylesheet">
    <link href="http://ift.tt/2iYEIjK" rel="stylesheet">
  </head>
  <body>
  <h2>Plesase select your division : </h2>
  <hr>
  <form method="POST" action="">
      
  </form>
  <hr>

The issue is that on display, all check box are present (5 checkbox) but they all have the same name (2021). I tried several combination but I cannot reach the target to have 1 checkbox associated with 1 year.

Could you please advise and help me if I'm doing something wrong ?

Kind regards,

Charles




Aucun commentaire:

Enregistrer un commentaire