Align radio buttons horizontally in django 1.11

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Align radio buttons horizontally in django 1.11

BIJAL MANIAR

Hi,

I'm trying to upgrade existing application from 1.4 to 1.11.
Below snippet of code to align radio buttons horizontally is working fine with django 1.4

from django.utils.safestring import mark_safe

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


class ApprovalForm(forms.Form):
    approval = forms.ChoiceField(choices=APPROVAL_CHOICES,
                 initial=0,
                 widget=forms.RadioSelect(renderer=HorizontalRadioRenderer),
                                 )
But getting below error with django 1.11. 
AttributeError: type object 'RadioSelect' has no attribute 'renderer'.
Is it due to introduction of template based widget rendering. Is there any link on how to implement it?

Any help would be appreciated. Thanks!

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/d389323e-3717-4819-914a-2b8cca751a1e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Align radio buttons horizontally in django 1.11

Tim Graham-2
You could use a custom widget template, e.g.

class HorizontalRadioSelect(forms.RadioSelect):
template_name = '...'

...

forms.ChoiceField(..., widget=HorizontalRadioSelect)

See https://docs.djangoproject.com/en/stable/ref/forms/renderers/ for more details.

Or you could add a CSS class and style the output using CSS.

widget=forms.RadioSelect(attrs={'class': 'inline'})

The admin uses this approach:
https://github.com/django/django/blob/c52ae33a0c0c0bbaa460607a8787e95fe983a2b9/django/contrib/admin/static/admin/css/forms.css#L42-L66
https://github.com/django/django/blob/c52ae33a0c0c0bbaa460607a8787e95fe983a2b9/django/contrib/admin/options.py#L64-L65

It was inadvertently broken in Django 1.11 but will be fixed in 1.11.1 (due out around May 1).
https://code.djangoproject.com/ticket/28059

On Wednesday, April 19, 2017 at 9:11:31 AM UTC-4, BIJAL MANIAR wrote:

Hi,

I'm trying to upgrade existing application from 1.4 to 1.11.
Below snippet of code to align radio buttons horizontally is working fine with django 1.4

from django.utils.safestring import mark_safe

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


class ApprovalForm(forms.Form):
    approval = forms.ChoiceField(choices=APPROVAL_CHOICES,
                 initial=0,
                 widget=forms.RadioSelect(renderer=HorizontalRadioRenderer),
                                 )
But getting below error with django 1.11. 
AttributeError: type object 'RadioSelect' has no attribute 'renderer'.
Is it due to introduction of template based widget rendering. Is there any link on how to implement it?

Any help would be appreciated. Thanks!

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/8a05c3fa-fd25-431d-8ea6-31f628e8fd1a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...