Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

268
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda