自定义 Django 管理结果列表

发布于 2024-09-10 05:08:50 字数 515 浏览 3 评论 0原文

如何更改 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 技术交流群。

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

发布评论

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

评论(1

浪菊怪哟 2024-09-17 05:08:50

没有“简单”的方法可以做到这一点,所以我必须首先问为什么你需要这样做。我不明白为什么 元素应该“显然”破坏您的设计。 元素等同于 ,只不过它默认具有额外的样式(通常为 font-weight:bold; text-align :中心;)。您应该能够在 CSS 中解释这一点。

也就是说,这里有一些值得关注的途径。 th/td 选择由 django.contrib.admin.templatetags.admin_list.py 的第 169 行(Django 1.2.1)确定。下面是它出现的上下文的摘要:

def items_for_result(cl, result, form):
    first = True
    for field_name in cl.list_display:
        # ...
        if (first and not cl.list_display_links) or field_name in cl.list_display_links:
            table_tag = {True:'th', False:'td'}[first]
            first = False
            # ...
            yield mark_safe(u'<%s%s><a href="%s"%s>%s</a></%s>' % (table_tag, row_class, url, ...)
        else:
            # ...
            yield mark_safe(u'<td%s>%s</td>' % (row_class, result_repr))

如您所见,没有明显的方法可以改变决定 table_tag 值的行为,因此您拥有的一些选项是:

  • 定义一个调用该模板的“items_for_result”模板标签并将生成值中的任何 替换为 。然后您可以在加载“admin_list”后覆盖“change_list.html”和{% load %}标记。
  • 编辑 Django 代码。不过以后你会后悔的。
  • 如果您同意表中的任何列都不是该项目编辑页面的链接(我无法想象您为什么会这样做),您可以在您的管理员中使用这个丑陋的黑客。 py

    admin.site.register(YourModel, YourModelAdmin)
    admin.site._registry[YourModel].list_display_links = ['not_a_field_name',]`
    

    由于管理模型仅验证一次(在调用 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 (usually font-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:

def items_for_result(cl, result, form):
    first = True
    for field_name in cl.list_display:
        # ...
        if (first and not cl.list_display_links) or field_name in cl.list_display_links:
            table_tag = {True:'th', False:'td'}[first]
            first = False
            # ...
            yield mark_safe(u'<%s%s><a href="%s"%s>%s</a></%s>' % (table_tag, row_class, url, ...)
        else:
            # ...
            yield mark_safe(u'<td%s>%s</td>' % (row_class, result_repr))

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:

  • Define a "items_for_result" templatetag that calls the one above and replaces any <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.
  • Edit the Django code. You'll regret it later though.
  • 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:

    admin.site.register(YourModel, YourModelAdmin)
    admin.site._registry[YourModel].list_display_links = ['not_a_field_name',]`
    

    Since the admin models are only validated once, which happens when register() is called, you can fetch the registered ModelAdmin afterwards and give it an invalid list_display_links property.

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