0

How to show a very simple statistical bar/graph bars on the right looking for a django package for easy implement i'd like it to be inside admin change page or model shown as an admin-inline Thanks

Community
  • 1
  • 1
Ohad the Lad
  • 1,889
  • 1
  • 15
  • 24

2 Answers2

5

You don't need any package to do this. You can show such graph with the two <div> and some CSS styling:

class StatsAdmin(admin.ModelAdmin):

    list_display = ('name', 'total', 'passed', 'failed', 'pass_fail')

    def pass_fail(self, obj):

        if not obj.total:
            return ('<div style="width: 100px; height: 10px; border:'
                                '1px solid black"></div>')

        percent_passed = int(obj.passed * 100.0 / obj.total)
        return ('<div style="width: 100px; height: 10px; '
                            'border: 1px solid black; background: red">'
                    '<div style="width: %spx; height: 10px; '
                                'background: green"></div>'
                '</div>' % percent_passed)

    pass_fail.allow_tags = True
    pass_fail.short_description = 'Pass / Fail'

Exmple screenshot

catavaran
  • 44,703
  • 8
  • 98
  • 85
0

pass_fail.allow_tags no longer available for django 1.9 and higher .

so i used the same code by changing it with mark_safe method

from django.utils.safestring import mark_safe
class StatsAdmin(admin.ModelAdmin):

    list_display = ('name', 'total', 'passed', 'failed', 'pass_fail')

    def pass_fail(self, obj):

        if not obj.total:
            return mark_safe('<div style="width: 100px; height: 10px; border:'
                            '1px solid black"></div>')

        percent_passed = int(obj.passed * 100.0 / obj.total)
        return mark_safe('<div style="width: 100px; height: 10px; '
                        'border: 1px solid black; background: red">'
                '<div style="width: %spx; height: 10px; '
                            'background: green"></div>'
            '</div>' % percent_passed)

    pass_fail.short_description = 'Pass / Fail'