What I have is a IntergerField that is represented by a set of Radio
Buttons in html. I want to lay out these buttons myself, and plus
there's javascript involved. I don't believe there's a way to do this
while letting the field render itself.
I need to have the "checked" attribute set for the appropriate radio
button, according to the bound field value on the form. I am trying
to do this manually -- to print out "checked" inside the input element
a manual comparison of the bound field's value, but it isn't working.
Note that I need to do this even when the form doesn't validate, so I
need to pull the value from form.field.initial or form.field.data
selectively, and so I have a template filter tag to do this.
Here's the code, isolated:
### a filter to get the bound field value independent of whether it
comes from initial/data
@register.filter
def boundvalue(boundfield):
"Copied from BoundField.as_widget"
if not boundfield.form.is_bound:
data = boundfield.form.initial.get(boundfield.name,
boundfield.field.initial)
if callable(data):
data = data()
else:
data = boundfield.data
return data
### My Template
<!---
Boundvalue: {{ genform.use_floating|boundvalue }} {# 'use_floating'
is the field name #}
{% ifequal form.use_floating|boundvalue "0" %}compared to "0"
(string){% endifequal %}
{% ifequal form.use_floating|boundvalue 0 %}compared to 0 (int){% endifequal %}
{% if form.use_floating|boundvalue %}compared true (boolean){% endif %}
--->
### The HTML page:
<!---
Boundvalue: 0
compared true (boolean)
--->
As you can see, I am getting some kind of string-typed output on the
page, so I've got *some* value. It casts to boolean true, but I
haven't been able to successfully compare it to 0.
If there's a better way to do this overall, I would also welcome
suggestions. I am not especially attached to solving this using
string comparisons but the form.data hash seems to force me down this
road.
Thank you
Elizabeth