JDavies
JDavies

Reputation: 2770

Thumbnail now showing in django admin list view

I have created a basic file upload app and i'd like to be able to view a thumbnail of the image on the django admin list view. I've tried implementing the code on this blog post - http://www.acedevs.com/blog/2011/07/11/django-admin-list-view-thumbnails/ ).

After adding the code, i've got the extra field called 'slide thumbnail' but when i upload an image it just sais 'none' so it's converting then image to a thumbnail and displaying it.

I don't have any errors so not sure exactly where i'm going wrong..

Here is my code and hopfully someone can shed some light.

MODLES:

from django.db import models from sorl.thumbnail.main import DjangoThumbnail import os

class File(models.Model):


    CATEGORY_CHOICES = (
        ('Image', 'Image'),
        ('Document', 'Document')
    )

    title = models.CharField(max_length=400, help_text="Enter the title of the file, this will appear on the listings page")




    file_type = models.CharField(choices=CATEGORY_CHOICES, help_text="Optional, but will help with filtering on listings page.", max_length=200, blank=True, null=True, default=None)


    image_upload = models.ImageField(upload_to="images/filesApp", height_field="image_height", width_field="image_width", blank=True, null=True)

    file_upload = models.FileField(upload_to="pdf/filesApp", blank=True, null=True)

    image_height = models.PositiveIntegerField(null=True, blank=True, editable=False)

    image_width = models.PositiveIntegerField(null=True, blank=True, editable=False)



    def slide_thumbnail(self, width=300, height=200):
        if self.image:
            thumb = DjangoThumbnail(self.image, (width, height))
            return '{img src="%s" /}' % thumb.absolute_url
        return '{img src="/media/img/admin/icon-no.gif" alt="False"}'
    slide_thumbnail.allow_tags = True
    def __unicode__(self):
        return u'Slide: %s - %sx%s' % (self.title, self.image_height, self.image_width)

ADMIN.PY

from django.contrib import admin
from models import *


def delete_selected(modeladmin, request, queryset):
    for element in queryset:
        element.delete()
delete_selected.short_description = "Delete selected elements"

class FileAdmin(admin.ModelAdmin):
    model = File
    actions = [delete_selected]

    list_display = ('title', 'file_type', 'slide_thumbnail')

admin.site.register(File, FileAdmin)

Thanks!

Upvotes: 2

Views: 1849

Answers (2)

JDavies
JDavies

Reputation: 2770

It turns out the variable was wrong in my function that was creating the thumbnail. Anyway, here is my finished code if anyone is interested in using thumbnails in the django admin list view.

models.py

from django.db import models from sorl.thumbnail.main import DjangoThumbnail import os

class File(models.Model):

CATEGORY_CHOICES = (
    ('Image', 'Image'),
    ('Document', 'Document')
)

title = models.CharField(max_length=400, help_text="Enter the title of the file, this will appear on the listings page")




file_type = models.CharField(choices=CATEGORY_CHOICES, help_text="Optional, but will help with filtering on listings page.", max_length=200, blank=True, null=True, default=None)


image_upload = models.ImageField(upload_to="images/filesApp", height_field="image_height", width_field="image_width", blank=True, null=True)

file_upload = models.FileField(upload_to="pdf/filesApp", blank=True, null=True)

image_height = models.PositiveIntegerField(null=True, blank=True, editable=False)

image_width = models.PositiveIntegerField(null=True, blank=True, editable=False)



def slide_thumbnail(self, width=300, height=200):
    if self.image_upload:
        thumb = DjangoThumbnail(self.image_upload, (width, height))
        return '<img src="%s" />' % thumb.absolute_url
    return '{img src="/media/img/admin/icon-no.gif" alt="False"}'
slide_thumbnail.allow_tags = True

def __unicode__(self):
    return u'File: %s - %sx%s' % (self.title, self.image_height, self.image_width)

admin.py

from django.contrib import admin
from models import *


def delete_selected(modeladmin, request, queryset):
    for element in queryset:
        element.delete()
delete_selected.short_description = "Delete selected elements"

class FileAdmin(admin.ModelAdmin):
    model = File
    actions = [delete_selected]

    list_display = ('title', 'file_type', 'slide_thumbnail')

admin.site.register(File, FileAdmin)

Please note: You will need sorl-thumbnail installed.

Hope this saves anyone the pain i had to go through!

Upvotes: 1

sandy
sandy

Reputation: 25

The TypeError is because of using FileField. ImageField is defined as

class ImageField(upload_to=None[, height_field=None, width_field=None, max_length=100, **options])

while FileField is defined as

class FileField(upload_to=None[, max_length=100, **options])

Upvotes: 0

Related Questions