Django Forms HorizontalRadioSelect Renderer not working.

1,236 views
Skip to first unread message

Shazia Nusrat

unread,
Jul 25, 2017, 3:53:13 AM7/25/17
to django...@googlegroups.com
Hi,

I need to select form values horizontally but couldn't get it work.

Following is the code:

class HorizontalRadioRenderer(forms.RadioSelect.renderer):
  def render(self):
    return mark_safe(u'\n'.join([u'%s\n' % w for w in self]))

class ResponseForm(models.ModelForm):
    class Meta:
        model = Response
        fields = ('interviewer', 'interviewee', 'conditions', 'comments')
    def __init__(self, *args, **kwargs):
        # expects a survey object to be passed in initially
        survey = kwargs.pop('survey')
        self.survey = survey
        super(ResponseForm, self).__init__(*args, **kwargs)
        self.uuid = random_uuid = uuid.uuid4().hex
        # add a field for each survey question, corresponding to the question
        # type as appropriate.
        data = kwargs.get('data')
        for q in survey.questions():
            if q.question_type == Question.TEXT:
                self.fields["question_%d" % q.pk] = forms.CharField(label=q.text,
                    widget=forms.Textarea)
            elif q.question_type == Question.RADIO:
                question_choices = q.get_choices()
                self.fields["question_%d" % q.pk] = forms.ChoiceField(label=q.text,
                    widget=forms.RadioSelect(renderer=HorizontalRadioRenderer),
                    choices = question_choices)

Error:

    class HorizontalRadioRenderer(forms.RadioSelect.renderer):
AttributeError: type object 'RadioSelect' has no attribute 'renderer'

Please advise

ecas

unread,
Jul 25, 2017, 4:57:59 AM7/25/17
to Django users
Which version of Django are you using? From 1.11 the widgets changed to a template mechanism.


Maybe this can help:
https://stackoverflow.com/questions/44187640/django-1-11-horizontal-choice-field

El dimarts, 25 juliol de 2017 9:53:13 UTC+2, Shazia Nusrat va escriure:

Shazia Nusrat

unread,
Jul 25, 2017, 8:10:58 AM7/25/17
to django...@googlegroups.com
I am using Djnago 1.11.1. I've tried template approach but perhaps I've missed out something. 

If you can provide me some workable example I will be really really thankful.

Regards,
Shazia

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d7280ade-f0c0-4c31-813f-1747f1f3cd29%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tim Graham

unread,
Jul 25, 2017, 3:27:14 PM7/25/17
to Django users
Replace renderer=HorizontalRadioRenderer with attrs={'class': 'inline'} and then style radio elements using CSS. contrib.admin uses this approach. Here's the CSS:

https://github.com/django/django/blob/d17eaa868cd6911197dcd8b096c4f0418c47007e/django/contrib/admin/static/admin/css/forms.css#L42-L66
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Shazia Nusrat

unread,
Jul 26, 2017, 7:47:09 AM7/26/17
to django...@googlegroups.com
Thank you so much for help. 

Regards,

Shazia

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages