Note that there are some explanatory texts on larger screens.

plurals
  1. POCrispy forms ignores initial value for radio button
    primarykey
    data
    text
    <p>I'm trying to conditionally set an initial value on a crispy forms InlineRadio field, and am getting ignored.</p> <p>I set the <code>initial</code> value like so:</p> <pre><code>class CustomWizardForm3(ModelForm): def __init__(self, user, *args, **kwargs): self.helper = FormHelper() self.helper.layout = Layout( Fieldset("", Div(ImageInlineRadios('sleeve_length'), css_class='span8 clearfix'), Div( CustomStyleDiv('armhole_edging_stitch', 'armhole_edging_height'), css_class="row-fluid") ) ) super(CustomWizardForm3, self).__init__(user, HELP_TEXT, *args, **kwargs) if self.instance.is_vest(): self.fields['sleeve_length'].initial = DC.SLEEVE_VEST class Meta: model = IndividualDesign fields = ('armhole_edging_stitch', 'armhole_edging_height', 'sleeve_length',) </code></pre> <p>In the model, <code>sleeve_length</code> is declared as:</p> <pre><code>sleeve_length= models.CharField( max_length = 20, choices = DC.SLEEVE_LENGTH_CHOICES, null = True, blank = True) </code></pre> <p>I've stepped through it in the debugger, and the <code>initial</code> attribute is set to what I expect. But when the radio buttons are generated, nothing is checked. What gives? Crispy and/or Django Forms is driving me mad.</p> <p>ETA: ImageInlineRadios is a simple subclass of the standard crispy thing.</p> <p>class ImageInlineRadios(InlineRadios):</p> <pre><code>def __init__(self, *args, **kwargs): self.span_class = kwargs.pop('span_class', 2) super(ImageInlineRadios, self).__init__(*args, **kwargs) template = 'radio_buttons.html' def render(self, form, form_style, context, template_pack='bootstrap'): import pdb; pdb.set_trace() context.update({'inline_class': 'inline', 'images': 'none', 'span_class':self.span_class}) return super(ImageInlineRadios, self).render(form, form_style, context) </code></pre> <p>In the radio_buttons.html template file, we have:</p> <pre><code>&lt;input type="radio" {% if choice.0|stringformat:"s" == field.value|stringformat:"s" %} checked="checked"{% endif %} </code></pre> <p>Does crispy not set <code>field.value</code> from <code>initial</code>? That part looks to me like what's in the radioselect.html template that comes with crispy, but if there's something else I should test, I'd love to know what it is.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload