#26505: django form dynamic attribute lookup
-------------------------+-------------------------------------------------
     Reporter:           |      Owner:  nobody
  igorbelo               |
         Type:  Bug      |     Status:  new
    Component:  Forms    |    Version:  1.9
     Severity:  Normal   |   Keywords:  django-forms custom-templatetag
 Triage Stage:           |  dynamic-attribute-lookup
  Unreviewed             |  Has patch:  0
Easy pickings:  0        |      UI/UX:  0
-------------------------+-------------------------------------------------
 I'm facing an issue, and can't figure out how to solve it.

 I have the following form in my application:
 {{{
 class ContentForm(ModelForm):
         class Meta:
             model = Content
             fields = ('url_1','url_2','url_3','url_4','url_5',)
             widgets = {
                 'url_1': forms.URLInput(attrs={'class': 'form-control',
 'maxlength': 100}),
                 'url_2': forms.URLInput(attrs={'class': 'form-control',
 'maxlength': 100}),
                 'url_3': forms.URLInput(attrs={'class': 'form-control',
 'maxlength': 100}),
                 'url_4': forms.URLInput(attrs={'class': 'form-control',
 'maxlength': 100}),
                 'url_5': forms.URLInput(attrs={'class': 'form-control',
 'maxlength': 100}),
             }
 }}}
 What I'm trying to do is to generate those url fields dynamically through
 a loop. I took [http://stackoverflow.com/questions/27324602/django-use-
 value-of-template-variable-as-part-of-another-variable-
 name/27324674#27324674 this] approach (which implements a template tag for
 dynamic attribute lookup):
 {{{
     {% for i in 12345|make_list %}
         {% with url='url_'|add:i %}
           {{ form|getattribute:url }}
         {% endwith %}
     {% endfor %}
 }}}
 However it's not working. Django doesn't render the input field and
 complains that my form object doesn't have an attribute called `url_1` for
 example. If I call the `url_1` attribute for the same form directly it
 works. I checked it many times and the form object id is the same in all
 contexts (template, view and custom template tag).

 The implementation of the `getattribute` template tag is the same as
 [http://stackoverflow.com/questions/844746/performing-a-getattr-style-
 lookup-in-a-django-template/ here].

 ps.: django version running is 1.9.2

--
Ticket URL: <https://code.djangoproject.com/ticket/26505>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.2f6e97930cd63784cb653170f8c4f2a4%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to