Ticket #5476: image-preview-in-admin.diff

File image-preview-in-admin.diff, 2.6 KB (added by vitja, 16 years ago)

preview image on model admin page

  • django/db/models/fields/__init__.py

     
    901901        return super(FloatField, self).formfield(**defaults)
    902902
    903903class ImageField(FileField):
    904     def __init__(self, verbose_name=None, name=None, width_field=None, height_field=None, **kwargs):
     904    def __init__(self, verbose_name=None, name=None, width_field=None, height_field=None, admin_preview=False, **kwargs):
    905905        self.width_field, self.height_field = width_field, height_field
     906        self.admin_preview = admin_preview
    906907        FileField.__init__(self, verbose_name, name, **kwargs)
    907908
    908909    def get_manipulator_field_objs(self):
  • django/contrib/admin/options.py

     
    173173            return db_field.formfield(**kwargs)
    174174
    175175        # For FileFields and ImageFields add a link to the current file.
    176         if isinstance(db_field, models.ImageField) or isinstance(db_field, models.FileField):
     176        if isinstance(db_field, models.ImageField):
     177            if db_field.admin_preview:
     178                kwargs['widget'] = widgets.AdminImageWidget
     179            else:
     180                kwargs['widget'] = widgets.AdminFileWidget
     181            return db_field.formfield(**kwargs)
     182        elif isinstance(db_field, models.ImageField) or isinstance(db_field, models.FileField):
    177183            kwargs['widget'] = widgets.AdminFileWidget
    178184            return db_field.formfield(**kwargs)
    179185
  • django/contrib/admin/widgets.py

     
    9393        output.append(super(AdminFileWidget, self).render(name, value, attrs))
    9494        return mark_safe(u''.join(output))
    9595
     96class AdminImageWidget(AdminFileWidget):
     97    """
     98    Just like AdminFileWidget but adds image to admin page.
     99    """
     100    def render(self, name, value, attrs=None):
     101        from django.conf import settings
     102        output = []
     103        if value:
     104            output.append('<img src="%s%s" /> <br />' % (settings.MEDIA_URL, value))
     105        output.append(super(AdminImageWidget, self).render(name, value, attrs))
     106        return mark_safe(u''.join(output))
     107   
     108
    96109class ForeignKeyRawIdWidget(forms.TextInput):
    97110    """
    98111    A Widget for displaying ForeignKeys in the "raw_id" interface rather than
Back to Top