# -*- coding: utf-8 -*-
from django import forms
from django.utils.translation import ugettext_lazy as _
from django.utils.safestring import mark_safe
from django.conf import settings
APPROVAL_CHOICES = (
(1, mark_safe(_('<img src="%(media_url)simg/thumbs_up.gif" alt="approve" title="approve">' %
{'media_url': settings.MEDIA_URL, },
))),
(2, mark_safe(_('<img src="%(media_url)simg/thumbs_down.gif" alt="disapprove" title="disapprove">' %
{'media_url': settings.MEDIA_URL, },
))),
(0, mark_safe(_('<img src="%(media_url)simg/question_mark.gif" alt="undecided" title="undecided">' %
{'media_url': settings.MEDIA_URL, },
))),
)
class HorizontalRadioRenderer(forms.RadioSelect.renderer):
"""renders horizontal radio buttons.
found here:
https://wikis.utexas.edu/display/~bm6432/Django-Modifying+RadioSelect+Widget+to+have+horizontal+buttons
"""
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),
)
Comments
Nice, but what happen if the icon's are in database? Thanks
#
You can have a betters solution if you DRY: Don't repeat yourself. There are three times "[HTML_REMOVED]". I would not put any markup into the choices. The html should be created in render().
#