django 管理统计 bar/graph。
django admin statistical bar/graph.
如何显示一个非常简单的统计数据bar/graph
寻找一个易于实施的django包
我希望它位于管理更改页面或显示为管理内联的模型中
谢谢
您不需要任何包来执行此操作。您可以使用两个 <div>
和一些 CSS 样式显示此类图表:
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'
pass_fail.allow_tags 不再适用于 django 1.9 及更高版本。
所以我通过使用 mark_safe 方法
更改它来使用相同的代码
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'
如何显示一个非常简单的统计数据bar/graph
您不需要任何包来执行此操作。您可以使用两个 <div>
和一些 CSS 样式显示此类图表:
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'
pass_fail.allow_tags 不再适用于 django 1.9 及更高版本。
所以我通过使用 mark_safe 方法
更改它来使用相同的代码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'