Monday, September 3, 2012

bound field object - dynamic forms

Hi all - not sure how to show the choicefield dropdown in my template?
Am generating a dynamic form (note: for issue in issues in forms.py) and appending the fields to self.issue_list
in the template I am looping over these fields ({% for issue in form.issue_list %}) and can write out the label {{ issue.label }} but if write out {{ issue }} I simply get the bound field object written to template (<django.forms.fields.ChoiceField object at 0x40d03d0>).
Question is how do I get the html dropdown to display in the template??


>>> forms.py      def __init__(self, item, *args, **kwargs):          super(ItemAnalysisForm, self).__init__(*args, **kwargs)          issue_choice = (('1', 'Reduced'),                          ('2', 'Maintained'),                          ('3', 'Increased')              )          issues = item.issue.all()            self.issue_list = []          self.a_list = []          # generate dynamic issue dropdowns...          for issue in issues:              self.fields['issue-' + str(issue.pk)] = forms.ChoiceField(label=issue.name, choices=issue_choice, required=False)              self.a_list.append(self.fields['issue-' + str(issue.pk)])              self.issue_list.append(self.fields['issue-' + str(issue.pk)])          self.fields['text'] = forms.CharField(label='text', required=True, widget=forms.Textarea(attrs={'rows':25,'cols':'100'}))          self.a_list.append(self.fields['text'])      >>> template      <form method="post" action="." name="form" id="item_analysis_form">{% csrf_token %}          <fieldset class="module">              <h2>Individual Issue Outcome:</h2>              <div class="form-row">                  {% for issue in form.issue_list %}                      <div class="form-row">                          {{ issue.label }}: {{ issue }}                      </div>                  {% endfor %}              </div>          </fieldset>          <fieldset class="module aligned ">              <h2>Comment:</h2>              <div class="form-row">                  {{ form.text }}</div>          </fieldset>        </form>

--
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/-/XSMbUGOJyvMJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment