Advertisement
kastielspb

Django admin widget

Oct 16th, 2019
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. from django.contrib.admin.widgets import AdminFileWidget
  2. from django.utils.html import mark_safe
  3.  
  4. from .utils import wrap_field_html
  5.  
  6. __all__ = (
  7.     'AdminImageWidget'
  8. )
  9.  
  10.  
  11. class AdminImageWidget(AdminFileWidget):
  12.     def render(self, name, value, attrs=None, renderer=None):
  13.         output = []
  14.         if value and getattr(value, "url", None):
  15.             image_url = value.url
  16.             file_name=str(value)
  17.             output.append(f'''
  18.                <a href="{image_url}" target="_blank">
  19.                    <img src="{image_url}" alt="{file_name}" style="display: block; max-width: 150px; max-height: 150px;"/>
  20.                </a>
  21.            ''')
  22.         output.append(super().render(name, value, attrs, renderer))
  23.         return mark_safe(wrap_field_html(''.join(output)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement