开发者

Only displaying one radio button widget from a Django RadioSelect

开发者 https://www.devze.com 2023-03-20 11:39 出处:网络
I am making a form with Django, and need to manually lay out the form in the HTML. I have one field that\'s a ChoiceField and the widget is a RadioSelect (i.e. radio button开发者_StackOverflow中文版s

I am making a form with Django, and need to manually lay out the form in the HTML. I have one field that's a ChoiceField and the widget is a RadioSelect (i.e. radio button开发者_StackOverflow中文版s rather than select drop down). There are 2 choices in this field (i.e. there will be 2 radio buttons when it's displayed).

If i do {{ form.fieldname }}, then it puts in both radio buttons there. I want to control where each radio button goes (since I need to put other things 'beside' each radio button, rather than just the textual value from the choices parameter).

I can manually enter the correct html <input type="radio" name="fieldname"…, however this won't reflect the value, if the field is set to the first choice then this HTML won't have selected="selected" on it.

Is there anyway, in the django template, to just put in the first radio button for a field? Something like this:

{{ form.fieldname.radio_button_0 }} (some of my custom html here)
Some more stuff {{ form.fieldname.radio_button_1 }} some more html here.


This page has information on how to iterate over each radio button: https://docs.djangoproject.com/en/dev/ref/forms/widgets/

See "class RadioSelect".


Try form.fieldname.widget.renderer[%CHOICE%]. For example if you have choices = (('o', 'one'),('t', 'two')), then valid %CHOICE% would be 'o' or 't'.

0

精彩评论

暂无评论...
验证码 换一张
取 消