如何在Django admin中包装文本(设置列宽) [英] How to wrap text in Django admin(set column width)

查看:43
本文介绍了如何在Django admin中包装文本(设置列宽)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型商品

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管理站点中的字段标题"(固定标题列的宽度).我该如何实现.请帮忙.

and I need to show the field 'title' wrapped in my Django admin site(fix a width for title column) . How can I achieve that. please help.

推荐答案

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

If I understand you correctly you need the list_display property of the Django's admin class.

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)

  • 请参见 https://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-asset-definitions
  • 您的管理模块:

    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

    Let's say, we're going to use the jquery.expander plugin https://github.com/kswedberg/jquery-expander

    然后您的"your_code.js"将如下所示:

    Then your "your_code.js" would look as follows:

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

    这篇关于如何在Django admin中包装文本(设置列宽)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆