niekas
niekas

Reputation: 9087

Django: How to render form field by html_name

How can I render specific form field if I know its html_name?

Lets say I have this form:

class MyForm(forms.Form):
    approve = forms.BooleanField()

Then form.fields['approve'].html_name == 'approve'. I tried in my template.html:

{{ form.fields.approve }}

But I get <django.forms.fields.BooleanField object at 0x2b2bf4503290> text rendered instead of input field.

Upvotes: 0

Views: 1077

Answers (1)

bruno desthuilliers
bruno desthuilliers

Reputation: 77892

form.fields holds the form.Field instances. You want the BoundField instances, which are accessible directly from the form instance using key access (in Python code) or dotted access (in template code). IOW:

in python:

form["approve"]

in templates :

{{ form.approve }}

Now note that it's not the html_name you have to use but the field name.

Upvotes: 1

Related Questions