Note that there are some explanatory texts on larger screens.

plurals
  1. POdjango form radio input layout
    primarykey
    data
    text
    <p>What is the "djangoy" way to approach this problem:</p> <p>In my form class, I have a forms.ChoiceField whose widget is a forms.RadioSelect widget, one of whose choices needs to be presented with an inline text input (which is also a field in the form). I'm using custom validation to ignore the text field when its radio choice is not selected. When rendered, I want it to appear like below:</p> <pre><code>&lt;ul&gt; &lt;li&gt;&lt;label for="id_rad_0"&gt;&lt;input type="radio" id="id_rad_0" value="none" name="rad" /&gt; No Textbox&lt;/label&gt;&lt;/li&gt; &lt;li&gt;&lt;label for="id_rad_1"&gt;&lt;input type="radio" id="id_rad_1" value="one" name="rad" /&gt; One Textbox: &lt;input type="text" name="bar" id="id_bar" /&gt;&lt;/label&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>However, I can't simply produce this in my template, because the radio choices are not exposed. I can't see a way to do this without tightly coupling my form to my template, or alternately, putting all of the presentation logic in the form class. What is the <em>right</em> way to solve this problem?</p> <p><strong>edit</strong></p> <p>I realize that the above might just be an obscure problem, but I'm not sure exactly what other information I can provide in order to inspire someone to help me with this. I'm a much better backend programmer than web designer, and I'm on this project alone, so maybe it's a lack of education - is what I described simply poor design? Should I just be designing this a different way? I'm really open to any suggestion here that will help me move past this.</p> <p><strong>edit 2</strong></p> <p>Per request, the current code, shortened to save sanity, names changed to protect the innocent:</p> <pre><code># forms.py from myapp.models import RatherComplicatedModel from django import forms class RatherComplicatedForm(forms.ModelForm): #various and sundry code... RADIO_CHOICES = ( ('none', "No Textbox"), ('one', "One Textbox: "), ) # although I've abbreviated the model, 'rad' does not appear in the model; # it merely provides input to the un-provided clean function rad = forms.ChoiceField(widget=forms.RadioSelect(),choices=RADIO_CHOICES) class Meta: model = RatherComplicatedModel </code></pre> <p>-</p> <pre><code># models.py from django.db import models class RatherComplicatedModel(models.Model): #some other stuff... bar = models.IntegerField(blank=True,null=True) </code></pre>
    singulars
    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.
 

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