[Django]-Remembering form field value on django model form

2👍

Try to mention the value of the input tag:

<input class="form-control" 
                             id="id_name" 
                             type="text" 
                             name="name" 
                             maxlength="128"
                             value="{{ form.name.value|default_if_none:'' }}"
                             placeholder="Your name..">
                        </div>
👤arocks

1👍

You’ve taken the responsibility for rendering those fields away from Django by simply hard-coding them in the HTML. That means that Django has no way of inserting the current values; not only when redisplaying after errors, but also if you have a form that modifies existing database content.

Don’t do it like that. I understand that you don’t want to just juse form.as_p, but there is a good middle ground: output each field in the template with {{ form.my_field }}. You can add relevant classes to the fields in the definition in forms.py, and then Django will take care of outputting it correctly.

Leave a comment