Django:显示DecimalField值

Rus*_*sty 3 python django django-templates django-models

我有一个模型DecimalField:

points_player1 = models.DecimalField(max_digits=3, decimal_places=1, blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)

当我在模板中显示此字段时,它始终显示类似0.01.0等的值.但我想使此行为更加用户友好:

  1. 如果用户输入,例如,是1它显示1(现在它的1.0).
  2. 如果用户输入1.5,则应显示1.5.
  3. 默认值是0,而不是0.0.

最好的方法是什么?是DecimalField对我的情况下,正确的选择?

Roh*_*han 12

您可以{{decimal_field|floatformat}}在模板中执行此操作 ,该模板将在必要时四舍五入并显示".0".

更多参考模板 - floatformat