Access specific ModelForm field option within array of ModelForms for use with .js

36 views
Skip to first unread message

jondykeman

unread,
May 1, 2012, 11:27:01 PM5/1/12
to django...@googlegroups.com
Hello, 

I have the following situation:

I am rendering an array of ModelForms to my template. ie. formArray = [<ModelForm1>, <ModelForm2>, <ModelForm3>]

In the template I display the forms with the following:

{% for form in formArray %}
<div class="clearfix">
{{ form }}
</div>
{% endfor %}

Previously I have accessed certain ModelForms using {% if forloop.counter == # %} to add some customization.

However, now I would like more detailed control of certain field tags within a given ModelForm. ie. ModelForm2 is a RadioSelect, and I would like to add an onChange="some function" to one of the Radio Buttons within this ModelForm. 

Is there a way to access this specific field in my implementation directly? Or, through the use of a custom template filter?

To give the larger picture my goal is to have an "Other" RadioButton that when selected shows a Other text input using javascript, but while using ModelForms.

As such, if there is an entirely different way to going about this I would love to hear it. 

Any help would be greatly appreciated!


JD



Kurtis Mullins

unread,
May 3, 2012, 2:27:19 PM5/3/12
to django...@googlegroups.com
You could do something along these lines:

{% for field in form %}
...
<!-- The Radio Field -->
{% ifequal field form.someSpecificField %}
<input type="radio"  onClick="..." name="{{ field.name }}" value="{{ field.value }} />
{%endif %}
...
{% endfor %}

(That syntax may not be perfect, I just typed it in real quick). Hopefully that helps a bit.




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/OVPO5oqiXc4J.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

jondykeman

unread,
May 3, 2012, 5:32:15 PM5/3/12
to django...@googlegroups.com
You're right! 

Thank you.

I have a custom template filter in the app to format the radio options a certain way and I can just add this in to pick out the ones needing the additional onChange.

JD
Reply all
Reply to author
Forward
0 new messages