M
M
maxclax2015-03-16 20:20:57
Django
maxclax, 2015-03-16 20:20:57

How to format output in Djanga admin?

Share your experience on how to format the data list output in the admin panel. For example, change the output of the standard date display, add some text somewhere. In my case, everything goes through additional functions in the class, and as for me, this is a mockery and shit code. It turns out that I am redefining the template in the logic and this should not be.

class EPSAdmin(BaseAdmin):
    form = EpsForm
    list_display = ('info_tag_1', 'info_tag_2', 'info_tag_3', 'info_tag_updated_at', 'is_active')

    # колонка 1
    def info_tag_1(self, obj):
        if not (obj.pk and obj.icon):
            return u'<table><tr><td></td><td><b>' + obj.name + '</b><br/>' + obj.smallname + '</td></table>'
        return u'<table><tr><td><img src="' + obj.icon.url + '"  width="32px"/></td><td><b>' + obj.name + '</b><br/>' + obj.smallname + '</td></table>'

    info_tag_1.short_description = _('eps.admin.info_tag_1.short_description')
    info_tag_1.allow_tags = True

    # колонка 2
    def info_tag_2(self, obj):
        if not obj.pk:
            return ''
        return _(
            'eps.admin.info_tag_2 %(currency)s %(commission_percent)s %(commission_min)s %(commission_max)s %(commission_extra)s.') % {
                   'currency': obj.currency.code,
                   'commission_percent': str(obj.commission_percent) + "%", 'commission_min': obj.commission_min,
                   'commission_max': obj.commission_max, 'commission_extra': obj.commission_extra}

    info_tag_2.short_description = _('eps.admin.info_tag_2.short_description')
    info_tag_2.allow_tags = True

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
un1t, 2015-03-16
@un1t

You can render the template separately render_to_string

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question