Skip to content

IntegerField with a 0 value is not rendered properly with TemplateHTMLRenderer #5767

Closed
@kgeorgy

Description

@kgeorgy

Steps to reproduce

Create a serializer with an IntegerField (or a ModelSerializer for a model with an IntegerField).

In the view, set the renderer to be TemplateHTMLRenderer.

If there is a zero value stored for the field into the DB, then the input field in the generated HTML has no value set:

<input name="x" class="form-control" type="number">

This make sense if the value was None but not if the value is 0.

Expected behavior

The generated HTML code should be for 0 value:

<input name="x" class="form-control" value="0" type="number">

Actual behavior

The following HTML code is generated:

<input name="x" class="form-control" type="number">

Suspicion

I suspect the problem is in input.html, in the line

<input name="{{ field.name }}" {% if style.input_type != "file" %}class="form-control"{% endif %} type="{{ style.input_type }}" {% if style.placeholder %}placeholder="{{ style.placeholder }}"{% endif %} {% if field.value %}value="{{ field.value }}"{% endif %} {% if style.autofocus and style.input_type != "hidden" %}autofocus{% endif %}>

The {% if field.value %}value="{{ field.value }}"{% endif %} may be evaluated as False because value is 0.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions