Use case: I’m overriding fieldset.html for one of my apps. I need to modify the behaviour of fields and how they appear. Source File
Problem Statement:
Here we are displaying fields via loop by default. But if I want to get the first field value. I’m unable to get the value of the first field. I’m sure it’s a basic thing But I also searched on the web but didn’t find any Solution.
That’s how I’m trying but that isn’t working.
{{ fieldset.0.0.field.value }}
Thanks.
fieldset.html by default.
{% for line in fieldset %}
<div class="form-row{% if line.fields|length == 1 and line.errors %} errors{% endif %}{% if not line.has_visible_field %} hidden{% endif %}{% for field in line %}{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% endfor %}">
{% if line.fields|length == 1 %}{{ line.errors }}{% endif %}
{% for field in line %}
<div{% if not line.fields|length == 1 %} class="fieldBox{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% if not field.is_readonly and field.errors %} errors{% endif %}{% if field.field.is_hidden %} hidden{% endif %}"{% elif field.is_checkbox %} class="checkbox-row"{% endif %}>
{% if not line.fields|length == 1 and not field.is_readonly %}{{ field.errors }}{% endif %}
{% if field.is_checkbox %}
{{ field.field }}{{ field.label_tag }}
{% else %}
{{ field.label_tag }}
{% if field.is_readonly %}
<div class="readonly">{{ field.contents }}</div>
{% else %}
{{ field.field }}
{% endif %}
{% endif %}
{% if field.field.help_text %}
<div class="help"{% if field.field.id_for_label %} id="{{ field.field.id_for_label }}_helptext"{% endif %}>
{{ field.field.help_text|safe }}
</div>
{% endif %}
</div>
{% endfor %}
</div>
{% endfor %}
value
doesn’t appear to be a property on a form field in the docs. I think you may be looking for data
, though this is likely the uncleaned data.
Did you get a solution for that?
field in html template is an instance of django.contrib.admin.helpers.AdminField And has following attributes:
The field.field itself is a django.forms.BoundField instance. So to get a value you may want to use fieldset.0.0.field.field.data though it’s not a perfect solution for this.
Welcome @polemeest !
We appreciate your contributions here, but please do not post images of code, templates, error messages, tracebacks, or other textual information.
Copy / paste the text into your post, between lines of three backtick - ` characters. This means you’ll have a line of ```, then the code (or template, etc), then another line of ```. The lines of ``` must be lines by themselves and not part of any other line. (And make sure you use the backtick - `, not the apostrophe - ' or quote - ".)