4

When I do the following inside an admin file:

photo = Photo.objects.get(original_image__exact=file_name) 
val = photo.admin_thumbnail.url

I get this error:

Caught DoesNotExist while rendering: Photo matching query does not exist.

Here is my class:

class AdminImageWidget(forms.FileInput):
    """
    A ImageField Widget for admin that shows a thumbnail.
    """

    def __init__(self, attrs={}, *args, **kwargs):
        super(AdminImageWidget, self).__init__(attrs)

    def render(self, name, value, attrs=None):
        output = []
        file_name = unicode(value)

        if file_name:
            photo = Photo.objects.get(original_image__exact=file_name) 
            val = photo.admin_thumbnail.url

            output.append(('<a target="_BLANK" href="%s">'
                           '<img src="%s" /></a> '
                           % (val, val)))
        output.append(super(AdminImageWidget, self).render(name, value, attrs))
        return mark_safe(u''.join(output))

However, if I do it in the shell (python manage.py shell), it works perfectly!

weird huh?

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
demux
  • 4,544
  • 2
  • 32
  • 56
  • Can you try the following line from shell and see if it works? `photo = Photo.objects.get(original_image__exact=file_name)` – Manoj Govindan Aug 24 '10 at 17:58
  • yes, I already did that. sorry about not being too clear about it. – demux Aug 25 '10 at 01:30
  • ok, it has nothing to do with using the console. If I copy the filename from the database ('photos/08-2010/placeholder2_2.png') and then write: `photo = Photo.objects.get(original_image__exact='photos/08-2010/placeholder2_2.png')`, I get the result I was hoping for. I just have to find out why the string is different. – demux Aug 25 '10 at 10:37

2 Answers2

1

What exactly are you trying to do?

You should find another way to grab the image name. This method 'render' is called all the time by Django, even when the field itself is invalid. e.g.:

  • I upload 'me.png' file in the admin.
  • Click save
  • Django finds that another field in the admin is incorrect and returns the form to me.
  • He calls the render method with 'me.png' as the value parameter.
  • You get an exception, since this models wasn't even saved yet.

There are other ways to get the filename, you could override the save method and get the object instance for example.

Cesar Canassa
  • 18,659
  • 11
  • 66
  • 69
  • I'm trying to get the admin_thumbnail url from an imagekit object. see: http://bitbucket.org/jdriscoll/django-imagekit/wiki/Home Could you maybe write some example code? :D – demux Aug 25 '10 at 17:52
0

I've solved the problem but I feel like there should be a more elegant solution.

class AdminImageWidget(forms.FileInput):
    """
    A ImageField Widget for admin that shows a thumbnail.
    """

    def __init__(self, attrs={}, *args, **kwargs):
        super(AdminImageWidget, self).__init__(attrs)

    def render(self, name, value, attrs=None):
        output = []

        file_name = unicode(value)

        if file_name:
            pattern = re.compile('.png', re.IGNORECASE)
            val = '/media/photos_cache/' + pattern.sub('_admin_thumbnail.png', file_name)

            output.append(('<a target="_BLANK" href="%s">'
                           '<img src="%s" /></a> '
                           % (val, val)))
        output.append(super(AdminImageWidget, self).render(name, value, attrs))
        return mark_safe(u''.join(output))

The problem is you'll have to pre-cache the thumbnails.

EDIT:

Strange... Now it works...

class AdminImageWidget(forms.FileInput):
    def __init__(self, attrs={}, *args, **kwargs):
        super(AdminImageWidget, self).__init__(attrs)

    def render(self, name, value, attrs=None):
        output = []

        file_name = unicode(value)

        if file_name:
            photo = Photo.objects.get(original_image=file_name)

            val = photo.admin_thumbnail.url

            output.append(('<a target="_BLANK" href="%s">'
                           '<img src="%s" /></a> '
                           % (val, val)))
        output.append(super(AdminImageWidget, self).render(name, value, attrs))
        return mark_safe(u''.join(output))
demux
  • 4,544
  • 2
  • 32
  • 56