Saturday, April 22, 2017

Re: Align radio buttons horizontally in django 1.11

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 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/8a05c3fa-fd25-431d-8ea6-31f628e8fd1a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment