In my flask project I have views.py, models.py (SQLAlchemy to postgres), and forms.py files. I have been defining forms in form.py an instantiating them from views.py.
I was able to create dynamic content for a SelectField, but having difficulty doing this for BooleanField. Here is what I am trying to do:
Create a form that retrieves players from a team (in db). For each player a BooleanField is created. If the 'active' field in the db is 1, the value is is checked(true), otherwise it is unchecked(false). Users of the system can check/uncheck any box and then submit the form.
I have no problem iterating over the players but I can't seem to figure out how to generate unique names for each BooleanField (if they have the same name I will only get one value back). I also need to dynamically pass the default value based on the active status of the user.
I did some research and saw a couple of ideas around matching one of my ORM models - but I'm not really trying to create a field entry to match every column in my ORM model for my players - just trying to pull some data and create a form on the fly.
Might be able to do this all within jinja2 templates but that doesn't seem to be the best approach...
OK - trying to get more basic now, still having a problem - BooleanField always returns False:
forms.py:
class myform(Form):
available = BooleanField('available')
submit = SubmitField('Save')
views.py:
form = myform(available=True)
webpage.html
Available: {{ form.available }}
When the webpage renders the Checkbox is checked (due to the available=True) which I am passing, but when I submit, value comes back as False).
What I did notice as strange is that even when the box is checked the rendered HTML - <input id="available" type="checkbox" value="y" name="available" checked="">
Checked="" doesn't make sense to me. I noticed that when I remove 'available=True' then there is no checked="". I would think proper HTML should be checked=checked..