渲染时捕获DoesNotExist:照片匹配查询不存在

发布于 2024-09-15 11:18:31 字数 1130 浏览 6 评论 0原文

当我在管理文件中执行以下操作时:

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

我收到此错误:

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

这是我的类:

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

但是,如果我在 shell 中执行此操作(python manage.py shell),它会完美运行!

很奇怪吧?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

漫雪独思 2024-09-22 11:18:31

你到底想做什么?

您应该找到另一种方法来获取图像名称。 Django 始终调用此方法“render”,即使字段本身无效。例如:

  • 我在管理员中上传“me.png”文件。
  • 点击保存,
  • Django发现管理中的另一个字段不正确,并将表单返回给我。
  • 他使用“me.png”作为值参数来调用渲染方法。
  • 你会得到一个例外,因为这个模型甚至还没有保存。

还有其他方法可以获取文件名,例如,您可以覆盖 save 方法并获取对象实例。

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.

笔芯 2024-09-22 11:18:31

我已经解决了这个问题,但我觉得应该有一个更优雅的解决方案。

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

问题是您必须预先缓存缩略图。

编辑:

奇怪...现在可以了...

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

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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文