Re: bound field object - dynamic forms

288 views
Skip to first unread message

Mario Gudelj

unread,
Sep 3, 2012, 5:52:38 PM9/3/12
to django...@googlegroups.com

Try {{ issue.0}} and {{ issue.1}}

On Sep 4, 2012 5:55 AM, "mjh" <martin.jam...@gmail.com> wrote:
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...@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.

mjh

unread,
Sep 5, 2012, 8:09:57 AM9/5/12
to django...@googlegroups.com
tried {{ issue.x }} x = 0,1,2,3 but nothing is displayed in the template

any other thoughts?

Mando

unread,
Sep 5, 2012, 11:15:29 AM9/5/12
to django...@googlegroups.com
are you passing it to the template?

Amyth Arora

unread,
Sep 5, 2012, 1:55:26 PM9/5/12
to django...@googlegroups.com
check if it is being passed to template.

{% if issue %}
  {{ issue }}
{% else %}
  Issue was not passed #Debug
{% endif %}

To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/up3KXt_tm2YJ.

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.



--
Thanks & Regards
----------------------------

Amyth [Admin - Techstricks]
Email - aroras....@gmail.com, ad...@techstricks.com
Twitter - @a_myth_________
http://techstricks.com/

mjh

unread,
Sep 5, 2012, 4:18:23 PM9/5/12
to django...@googlegroups.com
yes it is being passed to the template as I can access the label, choices, help_text, required items of the boundfield object...

i.e., {{ issue.label }} {{ issue.choices }} {{ issue.help_text }}

I can get everything it seems apart from the form object !!!

{{ issue }} writes to the template: <django.forms.fields.ChoiceField object at 0x40d03d0>


???

Tom Evans

unread,
Sep 6, 2012, 5:48:36 AM9/6/12
to django...@googlegroups.com
On Wed, Sep 5, 2012 at 9:18 PM, mjh <martin.jam...@gmail.com> wrote:
> yes it is being passed to the template as I can access the label, choices,
> help_text, required items of the boundfield object...
>
> i.e., {{ issue.label }} {{ issue.choices }} {{ issue.help_text }}
>
> I can get everything it seems apart from the form object !!!
>
> {{ issue }} writes to the template: <django.forms.fields.ChoiceField object
> at 0x40d03d0>
>
>

You want that fields forms.BoundField, not the forms.Field object you
currently have. A BoundField is bound with the specific data in the
form, especially if the form was submitted, and has things like error
messages and a proper label field associated with them (you are
outputting the raw label text currently).

Change form.issues to be a list of the ids of the fields you require,
rather than the forms.Field object and then use an attribute fetcher
template tag in the template to retrieve the bound field from the
form.

Eg using this dict_get template tag:

http://permalink.gmane.org/gmane.comp.python.django.user/106119

and storing the ids of the issues in form.issue_list

{% for issue_id in form.issue_list %}
{% with form|dict_get:issue_id as issue %}
<div class="form-row">
{{ issue.errors }}
{{ issue.label_tag }}: {{ issue }}
</div>
{% endwith %}
{% endfor %}

Hope that helps

Tom
Reply all
Reply to author
Forward
0 new messages