76π
This is in the source for photologue (see models.py
, slightly adapted to remove irrelevant stuff):
def admin_thumbnail(self):
return u'<img src="%s" />' % (self.image.url)
admin_thumbnail.short_description = 'Thumbnail'
admin_thumbnail.allow_tags = True
The list_display
bit looks identical too, and I know that works. The only thing that looks suspect to me is your indentation β the two lines beginning image_img
at the end of your models.py
code should be level with def image_img(self):
, like this:
def image_img(self):
if self.image:
return u'<img src="%s" />' % self.image.url_125x125
else:
return '(Sin imagen)'
image_img.short_description = 'Thumb'
image_img.allow_tags = True
12π
Update v. 1.9
Note that in Django v.1.9
image_tag.allow_tags = True
is depricated and you should use format_html(), format_html_join(), or mark_safe() instead
So your model.py should look like this:
...
from django.utils.html import mark_safe
def image_img(self):
if self.image:
return mark_safe('<img src="%s" />' % self.image.url_125x125)
else:
return '(Sin imagen)'
image_img.short_description = 'Thumb'
and in your admin.py add:
list_display= ('image_img','product',)
readonly_fields = ('image_img',)
and for adding it in the βEdit modeβ of your admin panel in your admin.py add:
fields = ( 'image_img', )
- [Django]-Get model's fields in Django
- [Django]-Django migrations β workflow with multiple dev branches
- [Django]-CSRF verification failed. Request aborted. on django
7π
Add a method in your model (models.py
):
def image_tag(self):
return u'<img src="%s" />' % <URL to the image>
image_tag.short_description = 'Image'
image_tag.allow_tags = True
and in your ModelAdmin (admin.py
) add:
readonly_fields = ('image_tag',)
- [Django]-Python (and Django) best import practices
- [Django]-Django: what is the difference (rel & field)
- [Django]-Is Tornado a replacement to Django or are they complementary to each other?
6π
Adding on to @dominic, I wanted to use this in multiple models, so I created a function that I could call in each model, inputing the image to be displayed.
For instance in one app I have:
from django.contrib import admin
from .models import Frontpage
from ..admin import image_file
class FrontpageAdmin(admin.ModelAdmin):
list_display = ('image_thumb', ...)
image_thumb = image_file('obj.image()')
admin.site.register(Frontpage, FrontpageAdmin)
with image
a function of Frontpage that returns an image.
In another app I have:
from django.contrib import admin
from .models import Exhibition
from ..admin import image_file
class ExhibitionAdmin(admin.ModelAdmin):
list_display = ('first_image_thumb', ...)
first_image_thumb = image_file('obj.related_object.image',
short_description='First Image')
admin.site.register(Exhibition, ExhibitionAdmin)
This allows me to specify the image object and the short_description
while keeping the boilerplate in another file. The function is:
from sorl.thumbnail import get_thumbnail
from django.conf import settings
def image_file(image, short_description='Image'):
def image_thumb(self, obj):
image = eval(image_thumb.image)
if image:
thumb = get_thumbnail(image.file, settings.ADMIN_THUMBS_SIZE)
return u'<img width="{}" height={} src="{}" />'.format(thumb.width, thumb.height, thumb.url)
else:
return "No Image"
image_thumb.__dict__.update({'short_description': short_description,
'allow_tags': True,
'image': image})
return image_thumb
- [Django]-Django get a QuerySet from array of id's in specific order
- [Django]-Django using get_user_model vs settings.AUTH_USER_MODEL
- [Django]-Pulling data to the template from an external database with django
3π
Since the allow_tags has been deprecated, you should use format_html
function. The example code look like this:
models.py
file:
class TestAdminModel(models.Model):
img = models.URLField()
admin.py
file:
from django.utils.html import format_html
class TestAdmin(admin.ModelAdmin):
list_display = ["id", "img", "thumbnail"]
def thumbnail(self, obj):
return format_html('<img src="{}" style="width: 130px; \
height: 100px"/>'.format(obj.img))
thumbnail.short_description = 'thumbnail'
admin.site.register(models.TestAdminModel, TestAdmin)
You can see more details from django documentation.
- [Django]-Migrating Django fixtures?
- [Django]-When to use get, get_queryset, get_context_data in Django?
- [Django]-Django South β table already exists
1π
With django-imagekit you can add any image like this:
from imagekit.admin import AdminThumbnail
@register(Fancy)
class FancyAdmin(ModelAdmin):
list_display = ['name', 'image_display']
image_display = AdminThumbnail(image_field='image')
image_display.short_description = 'Image'
# set this to also show the image in the change view
readonly_fields = ['image_display']
- [Django]-Disabled field is not passed through β workaround needed
- [Django]-How can I activate the unaccent extension on an already existing model
- [Django]-How do I set up Jupyter/IPython Notebook for Django?