I have a simple HTML form for which I was not using a Django form, but now I want to add a select.
The select would be most easily created as a Django ChoiceField
(as opposed to creating the select manually by looping, etc), however, it seems that it won't render properly without being bound/created within a Django form. (By not rendering properly, the browser shows <django.forms.widgets.Select object at 0x10e9a9d10>
)
Is there any way to do this?
You should use Django Form. But if you really don't want to use the form. You can generate HTML from ChoiceField in your view and pass the html to your template.
from django.forms import fields
my_choice = fields.ChoiceField(choices=((1, '1'), (2,'2')))
field_name = 'my_choice'
field_value = 1
my_choice_html = my_choice.widget.render(field_name, field_value)
return render(request, 'template.html', {'my_choice': my_choice_html})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With