I have a Django form which is passed to the templates through the view. In the form's __init__ I' m trying to add a parameter to each field, which is accessible from code, but not from the template.
forms.py
class GenForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
super(GenForm, self).__init__(*args, **kwargs)
for field in self.fields:
self.fields[field].dest = 'test_value'
help_text = self.fields[field].help_text
template
{% for field in form %}
{{ field.help_text }} || {{ field.dest }}
{% endfor %}
views.py
ret_dic['form'] = GenForm(request = request)
return render(request, 'template_file', ret_dic)
My goal would be to reach the dest value for the fields from the template, but it's not displayed at all. What is strange / interesting, is that I can reach the dest value from code:
ipython
gen = GenForm()
gen.fields['name'].dest
#results: test_value
My questions are:
The help_text I can reach from the code and also from the template. Should I somehow register it maybe?
Python: 3.4 Django: 1.9
The field that you access in the template is actually an instance of BoundField. You can access the actual underlying field via the .field attribute. So this would work:
{{ field.help_text }} || {{ field.field.dest }}