R
R
Roman Mindlin2017-10-16 11:31:48
Django
Roman Mindlin, 2017-10-16 11:31:48

How to get custom field attributes in a template?

I made my own class for the form field with additional attributes:

class ModelChoiceFieldWithLinks(forms.ModelChoiceField):

    def __init__(self, *args, **kwargs):
        self.add_url = kwargs.pop('add_url')
        self.edit_url = kwargs.pop('edit_url')

        super(ModelChoiceFieldWithLinks, self).__init__(*args, **kwargs)

Pass them the values:
factory = ModelChoiceFieldWithLinks(queryset=Factory.objects.all(),
                                     add_url=reverse_lazy('factory_add'), edit_url=reverse_lazy('factory_edit'),
                                     required=False,
                                     widget=forms.Select(attrs={'class': 'form-control'}))

I did not specifically add these values ​​to the context of the form, but the values ​​are visible there with the debugger. How to get them in the template?
I tried
field.add_urland field.field.add_url
there are no errors, but in both cases I get an empty value.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question