Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

263
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda