[Fixed]-Django admin list_display newline

31๐Ÿ‘

โœ…

You have to use br instead of a \n, and specify that this field is allowed to use html tags:

def example(self):
    return 'TYPE : %s<br>RATE : %s<br>FAMILY %s' % (self.type, 
                                                    self.rate, 
                                                    self.family)
example.allow_tags = True

Or you can use some more elegant HTML way of formatting your output (like wrapping each in a span element with a certain class, and add some css to make then render below each other).

๐Ÿ‘คOfri Raviv

10๐Ÿ‘

In Django 2/3, you should use format_html, since allow_tags has been deprecated. So, the example code becomes:

from django.utils.html import format_html

class MyModelAdmin(admin.ModelAdmin):
    list_display = ('example',)

    def example(self, obj):
        return format_html('TYPE : %s<br />RATE : %s<br />FAMILY %s' % (
            self.type, self.rate, self.family))
๐Ÿ‘คCloud Artisans

0๐Ÿ‘

You have to use br instead of a \n, and specify that this field is allowed to use html tags:

def example(self):
return โ€˜TYPE : %s
RATE : %s
FAMILY %sโ€™ % (self.type,
self.rate,
self.family)
example.allow_tags = True
Or you can use some more elegant HTML way of formatting your output (like wrapping each in a span element with a certain class, and add some css to make then render below each other).

๐Ÿ‘คApurbo Kumar

Leave a comment