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

2 Answers 2

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

0
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'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.