I think the easier way is just to write a custom widget which subclasses RadioSelect and override the render() method to however you want to output the HTML.
On Jan 19, 6:45 am, ekellner <ekell...@gmail.com> wrote: > Hi, I am having trouble working with a BoundField value in a template. > > 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---