使用 sorl 的 django 管理面板中的缩略图

发布于 2024-08-19 17:37:43 字数 444 浏览 4 评论 0原文

我试图让我在 Django 管理面板中上传的图片显示为缩略图而不是路径。我已经安装了 sorl 并且可以制作缩略图显示在我的视图中。

我找到了 2 个片段(http://www.djangosnippets.org/snippets/579/http://www.djangosnippets.org/snippets/934/)我曾尝试实现,但由于文档贫乏以及我对 Django 框架的理解尚浅,两次尝试都失败了。

有人可以提供一个简单的分步指南来指导我如何让它发挥作用吗?

谢谢!

I am trying to have the pictures I upload in the Django admin panel to show up as thumbnails instead of the path. I have sorl installed and can make thumbnails that show up in my views.

I have found 2 snippets (http://www.djangosnippets.org/snippets/579/ and http://www.djangosnippets.org/snippets/934/) that I have tried to implement, but both attempts have failed because of the meager documentation and my as yet shallow understanding of the Django framework.

Could someone please provide a dumbed-down step-by-step guide of how I can get this to work?

Thanks!

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

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

发布评论

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

评论(2

删除会话 2024-08-26 17:37:44

我知道这是一篇旧帖子,但我认为我可能找到了比上面的管理模板覆盖方法更短的方法(尽管这是一个非常棒的解决方案 - 我必须在另一种情况下使用它)。这只是直接调用 sorl 函数来生成缩略图。

在 myapp/admin.py 中,

from myapp import models
from sorl.thumbnail import default
ADMIN_THUMBS_SIZE = '60x60'

class MyModelAdmin(admin.ModelAdmin):
    model = models.MyModel
    list_display = ['my_image_thumb', 'my_other_field1', 'my_other_field2', ]

    def my_image_thumb(self, obj):
        if obj.image:
            thumb = default.backend.get_thumbnail(obj.image.file, ADMIN_THUMBS_SIZE)
            return u'<img width="%s" src="%s" />' % (thumb.width, thumb.url)
        else:
            return "No Image" 
    my_image_thumb.short_description = 'My Thumbnail'
    my_image_thumb.allow_tags = True

我基于以下问题,但添加了 sorl 功能。

Django 管理和显示缩略图

I know this is an old post, but I think I may have found a shorter way than the admin template override method above (though that is a pretty awesome solution - I'll have to use it in another circumstance). This simply does a call directly to the sorl function to generate the thumbnail.

In myapp/admin.py

from myapp import models
from sorl.thumbnail import default
ADMIN_THUMBS_SIZE = '60x60'

class MyModelAdmin(admin.ModelAdmin):
    model = models.MyModel
    list_display = ['my_image_thumb', 'my_other_field1', 'my_other_field2', ]

    def my_image_thumb(self, obj):
        if obj.image:
            thumb = default.backend.get_thumbnail(obj.image.file, ADMIN_THUMBS_SIZE)
            return u'<img width="%s" src="%s" />' % (thumb.width, thumb.url)
        else:
            return "No Image" 
    my_image_thumb.short_description = 'My Thumbnail'
    my_image_thumb.allow_tags = True

I based this off of the following question but added the sorl functionality.

Django admin and showing thumbnail images

执着的年纪 2024-08-26 17:37:44

是的 :) 我可以 ;)

首先,您需要创建一个处理缩略图的自定义模板标签:

from django.template import Library
from django.utils.safestring import mark_safe
from django.contrib.admin.templatetags.admin_list import result_headers

register = Library()

def results(cl):
    out = []
    for item in cl.result_list:
        url = cl.url_for_result(item)
        code = '<a href="%(url)s">%(img)s</a> <div><a href="%(url)s">%(title)s</a></div>' % {
            'url': url,
            'img': item.preview.thumbnail_tag,
            'title': item.title,
        }
        out.append(mark_safe(code))

    return out

def gallery_result_list(cl):    
    return {'cl': cl,
            'result_headers': list(result_headers(cl)),
            'results': results(cl)}
result_list = register.inclusion_tag("admin/app_name/model/change_list_results.html")(gallery_result_list)

其中 item.preview.thumbnail_tag 是 sorl 创建的缩略图 :)
[我从默认模板标签中获取了原始代码]

其次,您需要为您的模型创建一个模板(使用新的自定义模板标签),它必须位于此目录架构中:
templates_dir/admin/app_name/model/change_list.html

并具有以下代码:

{% extends "admin/change_list.html" %}
{% load adminmedia admin_list my_admin_tags i18n %}

{% block result_list %}
    {% if action_form and actions_on_top and cl.full_result_count %}{% admin_actions %}{% endif %}
    {% gallery_result_list cl %}
    {% if action_form and actions_on_bottom and cl.full_result_count %}{% admin_actions %}{% endif %}
{% endblock %}

正如您在标记函数中看到的那样,您需要创建另一个模板(称为change_list_result.html)以正确显示图像:

<style>
td.page { text-align: center; }
td.page a { font-weight: bold; }
</style>
{% if results %}
<table cellspacing="0">
<tbody>
<tr>
{% for result in results %}
    <td class="page">
        {{ result }}
    </td>
    {% if forloop.counter|divisibleby:3 %}
</tr><tr>
    {% endif %}
{% endfor %}
</tr>
</tbody>
</table>
{% endif %}

所以最后您'将有 3 个文件:

  1. templates_dir/admin/app_name/model_name/change_list.html
  2. templates_dir/admin/app_name/model_name/change_list_result.html
  3. your_project/app_name/templatetags/my_admin_tags.py

当然,必须将 templatetags 添加到 settings 中的 INSTALLED_APP 中; )

这就是全部;)希望这会有所帮助。

Yeah :) I can ;)

First you need to create a custom template tag that handles the thumbnail:

from django.template import Library
from django.utils.safestring import mark_safe
from django.contrib.admin.templatetags.admin_list import result_headers

register = Library()

def results(cl):
    out = []
    for item in cl.result_list:
        url = cl.url_for_result(item)
        code = '<a href="%(url)s">%(img)s</a> <div><a href="%(url)s">%(title)s</a></div>' % {
            'url': url,
            'img': item.preview.thumbnail_tag,
            'title': item.title,
        }
        out.append(mark_safe(code))

    return out

def gallery_result_list(cl):    
    return {'cl': cl,
            'result_headers': list(result_headers(cl)),
            'results': results(cl)}
result_list = register.inclusion_tag("admin/app_name/model/change_list_results.html")(gallery_result_list)

where item.preview.thumbnail_tag is the thumnail created by sorl :)
[I got the original code from the default template tag]

Second you need to create a template for your model (that uses the new custom template tag), it must be in this directory schema:
templates_dir/admin/app_name/model/change_list.html

and have the following code:

{% extends "admin/change_list.html" %}
{% load adminmedia admin_list my_admin_tags i18n %}

{% block result_list %}
    {% if action_form and actions_on_top and cl.full_result_count %}{% admin_actions %}{% endif %}
    {% gallery_result_list cl %}
    {% if action_form and actions_on_bottom and cl.full_result_count %}{% admin_actions %}{% endif %}
{% endblock %}

as you can see in the tag function you need to create one more template (called change_list_result.html) for display the image correctly:

<style>
td.page { text-align: center; }
td.page a { font-weight: bold; }
</style>
{% if results %}
<table cellspacing="0">
<tbody>
<tr>
{% for result in results %}
    <td class="page">
        {{ result }}
    </td>
    {% if forloop.counter|divisibleby:3 %}
</tr><tr>
    {% endif %}
{% endfor %}
</tr>
</tbody>
</table>
{% endif %}

so at the end you'll have 3 files:

  1. templates_dir/admin/app_name/model_name/change_list.html
  2. templates_dir/admin/app_name/model_name/change_list_result.html
  3. your_project/app_name/templatetags/my_admin_tags.py

and, of course, templatetags must be added to INSTALLED_APP in settings ;)

this is all ;) Hope this can be helpful.

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