简体   繁体   English

Django admin list_display 在鼠标上显示全文 hover

[英]Django admin list_display show full text on mouse hover

There is a TextField named address .有一个名为address的 TextField 。 It usually contains more than 100 characters.它通常包含 100 多个字符。 I can truncate the string but I want the following to happen: Full text should appear on mouse hover on the truncated part.我可以截断字符串,但我希望发生以下情况: 完整文本应出现在鼠标 hover 上截断部分上。

If you don't need to edit the address in list_display then you can just create a readonly field and use that:如果您不需要编辑list_display中的地址,那么您可以创建一个只读字段并使用它:

from django.utils.html import format_html

class MyAdmin(admin.ModelAdmin):
    class Media:
         css = {
             'all': ("styles.css",)
         }

    list_display=(
        ...other fields,
        list_display_address,
    )

    def list_display_address(self, instance):
        return format_html(
            '<span class="truncated_address" title="{}">{}</span>', 
            instance.address,
            instance.address
        )

  • The way a readonly field works is by returning the value of a method on the modelAdmin.只读字段的工作方式是返回 modelAdmin 上的方法的值。
  • The title attribute sets the mouse over text. title 属性设置鼠标悬停在文本上。
  • By adding the following to our styles.css we can truncate the address if it's too long.通过将以下内容添加到我们的styles.css中,如果地址太长,我们可以截断地址。 (I'm assuming it is on a single line): (我假设它在一行上):
.truncated_address{
  text-overflow: ellipsis;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM