Django admin panel thumbnails using sorl

I am trying to upload photos that I upload to the Django admin panel to display them as thumbnails instead of paths. I installed sorl and can create thumbnails that appear in my views.

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

Can someone please provide a step-by-step guide on how I can make this work?

Thank!

+3
source share
2 answers

Yes I can;)

First you need to create your own 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 source code from the default template tag]

Secondly, you need to create a template for your model (which uses the new custom template tag), it should be in this directory scheme: 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 another template (the so-called change_list_result.html) for the image to display 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 will have 3 files:

  • templates_dir / admin / app_name / model_name / change_list.html
  • templates_dir//app_name/model_name/change_list_result.html
  • your_project/app_name/templatetags/my_admin_tags.py

, , templatetags INSTALLED_APP ;)

;) , .

+4

, , , , ( - ). 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

+13

Source: https://habr.com/ru/post/1730898/


All Articles