如何在Django管理后台中换行文本(设置列宽)

6

enter image description here我有一个名为Item的模型

class Item(models.Model):
    id = models.IntegerField(primary_key=True)
    title = models.CharField(max_length=140, blank=True)
    description = models.TextField(blank=True)
    price = models.DecimalField(max_digits=12, decimal_places=2, blank=True, null=True)

和我的模型管理器

class ItemAdmin(admin.ModelAdmin):
   list_display = ['item_view', 'description', 'item_price', 'seller_view', 'added_on']
   actions = ['add_to_staff_picks']
   search_fields = ('description', 'title')

   def item_view(self, obj):
       item = obj
       url = reverse('admin:%s_%s_change' % ('adminuser', 'item'), args=(item.id,))
       if item.is_active:
          return '<font color="green">%s</font>' % (base64.b64decode(item.title))
       return '<font color="red">%s</font>' % (base64.b64decode(item.title))
       item_view.allow_tags = True
       item_view.short_description = 'Title'

我需要在我的Django管理站点中展示“标题”字段(为标题列设置固定宽度),我该如何实现呢?请帮忙。
4个回答

3

在你的中添加一个功能,然后在中调用该功能。

#Model
def shortTitle(self):
    return str(self.title)[:50]

#modelAdmin
class ItemAdmin(admin.ModelAdmin):
   list_display = ['shortTitle', ...

这会截断文本但不强制换行。 - geoidesic

2

如果我理解正确,您需要Django的管理类的list_display属性。

"admin.py"示例

from django.contrib import admin

from path.to.your.app.models import Item

class ItemAdmin(admin.ModelAdmin):
    """
    Your ``Item`` model admin.
    """
    # List here all the fields that are to be displayed in the listing view.
    list_display = ('title', 'description',)

admin.site.register(Item, ItemAdmin)

更新的答案(2014年8月8日)

您的管理模块:

from django.conf import settings

class ItemAdmin(admin.ModelAdmin):
    # Some other code
    class Media:
        js = (
            '{0}js/jquery-1.10.2.min.js'.format(settings.STATIC_URL),
            '{0}js/jquery.expander.min.js'.format(settings.STATIC_URL),
            '{0}your_app/js/your_code.js'.format(settings.STATIC_URL),
            )

假设我们要使用jquery.expander插件https://github.com/kswedberg/jquery-expander
那么你的 "your_code.js" 代码应如下所示:
;
$(document).ready(function() {
   // Assuming that your element that would be wrapped comes as second td (column).
   // If not, adjst the nth-child(2). 
   $('#result_list tbody tr td:nth-child(2)').each(function(e) {
        $(this).expander({
            slicePoint:       50,  // default is 100
            expandSpeed: 0,
            expandEffect: 'show',
            collapseSpeed: 0,
            collapseEffect: 'hide',
            expandPrefix:     ' ', // default is '... '
            expandText:       '[...]', // default is 'read more'
            userCollapseText: '[^]'  // default is 'read less'
        });
   });
});

不需要。我已经添加了list_display。但是假设我添加了一个长度为400的段落,那不是很大吗?我想要像“关于**查看更多......**”这样包装我的描述。 - itsme
谢谢你,Arthur。你让我的一天变得美好.. :) 我已经使用了django.templatetags.static中的import static,而不是导入设置,:) - itsme

0

替代答案

list_display = ('title_name', 'description',)


 def title_name(self, obj):
    if obj.tracking_no:
        line_length = 20 
        lines = [obj.title[i:i+line_length] + '\n' for i in range(0, len(obj.title), line_length)]  
        return ''.join(lines)
    return obj.title_name

track.short_description = "title"

1
目前你所写的回答不够清晰,请编辑并添加更多细节以帮助其他人理解它如何回答问题。你可以在帮助中心中找到有关如何撰写好的回答的更多信息。 - Community

0

这是使用HTML的答案。您可以将其添加为ModelAdmin类中的方法:

    def title_(self, obj):
        from django.utils.html import format_html
        return format_html(
            f'<div style="width: 100px; word-wrap: break-word">{obj.title}</div>'
        )

然后您将title_添加到您的list_display声明中,而不是title


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接