Tuesday, July 25, 2017

Django Forms HorizontalRadioSelect Renderer not working.

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

--
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-users@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/CAD83tOy6AMDmVZ9WmEY7Z9zhzqswSpa%2BejZiGUvrUkwW1y8Tuw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment