如何在Django admin中包装文本(设置列宽)

时间:2014-08-07 12:15:56

标签: python django django-admin word-wrap

enter image description here我有一个模型项目

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管理站点中显示字段'title'(修复title列的宽度)。我怎样才能做到这一点。请帮忙。

2 个答案:

答案 0 :(得分:2)

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

示例&#34; admin.py&#34;

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-08-08)

您的管理模块:

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

然后你的&#34; your_code.js&#34;看起来如下:

;
$(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'
        });
   });
});

答案 1 :(得分:2)

'<h3 class ="lblProductQuantity">' + 'Quantity:' + ' ' + '<span class=' + className + '>' + ajProductDataFromServer[i].quantity + '</span>' + '</h3>' 添加一项功能,然后在Model中调用该功能

modelAdmin