是的 :) 我可以 ;)
首先你需要创建一个处理缩略图的自定义模板标签:
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 个文件:
- templates_dir/admin/app_name/model_name/change_list.html
- templates_dir/admin/app_name/model_name/change_list_result.html
- your_project/app_name/templatetags/my_admin_tags.py
当然,必须在设置中将模板标签添加到 INSTALLED_APP ;)
这就是全部;)希望这会有所帮助。