自定义 Django 管理结果列表
如何更改 Django 管理结果列表的输出结果?我一直在研究 change_result_list.html 模板文件,但我能找到的只是:
{% for item in result %}{{ item }}{% endfor %}
它将输出类似以下内容:
<tr>
<td>
<input type="checkbox" class="action-select" value="2" name="_selected_action" />
</td>
<th>
<a href="1/">Lorem Ipsum</a>
</th>
</tr>
显然,第 th 元素破坏了我的设计。有没有办法把它改成td?
How can I change the output result from Django Admin Result List? I've been looking into the change_result_list.html template file but all I can find is :
{% for item in result %}{{ item }}{% endfor %}
Which will be outputting something like :
<tr>
<td>
<input type="checkbox" class="action-select" value="2" name="_selected_action" />
</td>
<th>
<a href="1/">Lorem Ipsum</a>
</th>
</tr>
Obviously, the th element breaks my design. Is there anyway to change it into td?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有“简单”的方法可以做到这一点,所以我必须首先问为什么你需要这样做。我不明白为什么
元素应该“显然”破坏您的设计。
元素等同于
,只不过它默认具有额外的样式(通常为
font-weight:bold; text-align :中心;
)。您应该能够在 CSS 中解释这一点。也就是说,这里有一些值得关注的途径。 th/td 选择由 django.contrib.admin.templatetags.admin_list.py 的第 169 行(Django 1.2.1)确定。下面是它出现的上下文的摘要:
如您所见,没有明显的方法可以改变决定 table_tag 值的行为,因此您拥有的一些选项是:
替换为
。然后您可以在加载“admin_list”后覆盖“change_list.html”和{% load %}标记。
如果您同意表中的任何列都不是该项目编辑页面的链接(我无法想象您为什么会这样做),您可以在您的
管理员中使用这个丑陋的黑客。 py
:由于管理模型仅验证一次(在调用
register()
时发生),因此您可以在之后获取已注册的ModelAdmin
并给它一个无效的list_display_links
属性。There's no "easy" way to do it, so I must first ask why exactly you need to do this at all. I don't see why the
<th>
element should "obviously" break your design. The<th>
element is equivalent to<td>
, except that is has extra styling by default (usuallyfont-weight: bold; text-align: center;
). You should be able to account for this in your CSS.That said, here are some avenues to look at. The th/td choice is determined on line 169 (Django 1.2.1) of
django.contrib.admin.templatetags.admin_list.py
. Here's a snipped summary of the context it appears in:As you can see, there's no obvious way to alter the behaviour that determines the value of table_tag, so some of the options you have are:
<th>
s in the yielded values with<td>
s. Then you can override "change_list.html" and {% load %} the tag after "admin_list" is loaded.If you're OK with none of the columns in the table being a link to the edit page for the item (I can't imagine why you would), you can use this ugly hack in your
admin.py
:Since the admin models are only validated once, which happens when
register()
is called, you can fetch the registeredModelAdmin
afterwards and give it an invalidlist_display_links
property.