Created
January 17, 2017 13:08
-
-
Save jorgeas80/924a99716532a68abe1a70aa8cf9a1bb to your computer and use it in GitHub Desktop.
Django widget to preview images uploaded using a file input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from django.utils.safestring import mark_safe | |
class AdminImageWidget(forms.FileInput): | |
""" | |
A ImageField Widget for admin that shows a thumbnail. | |
Taken from https://djangosnippets.org/snippets/1580/ | |
""" | |
def __init__(self, attrs={}): | |
super(AdminImageWidget, self).__init__(attrs) | |
def render(self, name, value, attrs=None): | |
output = [] | |
if value and hasattr(value, "url"): | |
output.append(('<a target="_blank" href="%s">' | |
'<img src="%s" style="height: 250px;" /></a> <br /><br />' | |
% (value.url, value.url))) | |
output.append(super(AdminImageWidget, self).render(name, value, attrs)) | |
return mark_safe(u''.join(output)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment