widgets.py 924 B

12345678910111213141516171819202122232425
  1. from django.forms.widgets import RadioChoiceInput, RadioFieldRenderer, RendererMixin, Select
  2. from django.forms.utils import flatatt
  3. from django.utils.html import format_html
  4. import logging
  5. logger = logging.getLogger('django')
  6. class ThumbnailRadioChoiceInput(RadioChoiceInput):
  7. def tag(self, attrs=None):
  8. img_attrs = {
  9. 'class': 'thumbnail-radiobutton',
  10. 'src': self.choice_label,
  11. 'alt': self.choice_value,
  12. }
  13. thumbnail = format_html('<img {} />', flatatt(img_attrs))
  14. attrs['class'] = attrs.get('class', '') + ' img-radio-button'
  15. self.choice_label = ''
  16. return super().tag(attrs) + thumbnail
  17. class ThumbnailRadioFieldRenderer(RadioFieldRenderer):
  18. choice_input_class = ThumbnailRadioChoiceInput
  19. class ThumbnailRadioSelect(RendererMixin, Select):
  20. renderer = ThumbnailRadioFieldRenderer