I need to display a simple select input in a template.
This input is based on a selection of objects and should have an selected value.
It will not be used for any post to the server.
staffs = Staff.objects.filter(staff__company=comp)
selected_staff = staffs[0]
staffs are people that I need to display in the selection
I find this input quite verbose so I’m wondering if, instead of writing the input manually in the template, I should take advantage of a little form.
So the question is, should I write the <option> input manually or should I use the builtin widget ?
Using a built-in is always better. You can also use
Field.initialto set an initial value for the field. Documetation is hereDefining an option and set it on the template (probably using javascript) is much more harder.
UPDATE:
This is how you handle the Form Class. But overriding base Form class using
forms.ChoiceFieldis not a good choice since you use a queryset for defining choices. So use aModelChoiceFieldinsteadDjango will handle the rest of the validation and initialization of the form…