Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

264
Views
Adding a parameter to the fields of a django form which is accessible from the templates

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:

  1. how could I reach the dest variable from the template?
  2. why is the dest value reachable from code, but not from template?

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

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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 }}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!