Задать вопрос

Тел: +7 965 3737 888

449

Просмотров

1

Ответов

ImageField for admin with thumbnail

<p>AdminImageWidget is a ImageField Widget for admin that shows a thumbnail.</p>
<p>Usage example on a form:</p>
class IconForm(forms.ModelForm):
    icon = forms.ImageField(label='icon', widget=AdminImageWidget)

Вопрос полезен? Да0/Нет0
file_2874.py(716.0б)
None

Ответы (1):

Ответyolk:23.10.2013
Ответ полезен? Да0/Нет0

Thank you for this! I'm using this with Sorl.Thumbnails for thumbs; it was easy to add (just two more lines!)

from sorl.thumbnail import get_thumbnail

class AdminImageWidget(forms.FileInput):

  """ A ImageField Widget for admin that shows a thumbnail. """

  def __init__(self, attrs={}):
    super(AdminImageWidget, self).__init__(attrs)

  def render(self, name, value, attrs=None):

    output = []
    if value and hasattr(value, 'url'):
      thumb = get_thumbnail(value, 'x28', crop='center', format='PNG')
      output.append('<a target="_blank" href="%s"><img src="%s" style="height: 28px;" /></a> '
                     % (value.url, thumb.url))
    output.append(super(AdminImageWidget, self).render(name, value, attrs))
    return mark_safe(u''.join(output))