Search code examples
djangodjango-widgetimagekitdjango-imagekit

Caught DoesNotExist while rendering: Photo matching query does not exist


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?


Solution

  • 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))