Reputation: 881
I'm new to django. look at the following code in my form.py:
class ProfileForm(forms.Form):
name = forms.CharField(label=_("first name"))
lname = forms.CharField(label=_("last name"))
phone= forms.CharField(label=_("phone"))
address = forms.CharField(label=_("address"))
def categorize_fields(self):
categorized_fields = [
[ self.fields["name"], self.fields["lname"] ] ,
[ self.fields["phone"], self.fields["address"] ]
]
return categorized_fields
in my form render I have the following code which does not work properly:
{% for field_set in form.categorize_fields %}
{% for field in field_set %}
{{ field.label }}
{{ field }}
{% endfor %}
{% endfor %}
field.label
is working correctly but {{ field }}
is not showing the HTML rendered and instead is showing this:
<django.forms.fields.CharField object at 0x000012661591CA90>
but if I iterate through the main form passed to form_render.html, everything works fine:
{% for field in form.visible_fields %}
{{ field.label_tag }}
{{ field }}
{% endfor %}
how can I solve that? thanks
Upvotes: 1
Views: 403
Reputation: 600059
self.fields
contains references to the unbound field objects. If you want access to the actual bound fields, you need to index on self
directly:
categorized_fields = [
[ self["name"], self["lname"] ] ,
[ self["phone"], self["address"] ]
]
However, I do not recommend doing this. Instead, use a third-party library like django-crispy-forms.
Upvotes: 1