呀:)我可以;)
首先,你需要創建處理縮略圖的自定義模板標籤:
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_d IR /管理/ APP_NAME /模型/ 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 /管理/應用程序_name/MODEL_NAME/change_list.html
- templates_dir /管理/ APP_NAME /模型名稱/ change_list_result.html
- your_project/APP_NAME/templatetags/my_admin_tags.py
,當然,templatetags必須添加到INSTALLED_APP在設置;)
這是所有;)希望這可能會有所幫助。