djangoforms

Pass the url view in the forms label


How to correctly pass the view name in 'label'? My form looks like that:

class DocumentationForm(forms.Form):
    documentation = forms.BooleanField(label='I accept the terms and <a href="{%s}">conditions</a>.' %('app:documentation'),
                                       initial=False)

    def clean_website_rules(self):
        data = self.cleaned_data['documentation']
        if not data:
            raise forms.ValidationError("Please accept the terms and privacy policy.")
        else:
            return data

When I click on the link, something like that is created. host:name/data_1/data_2/data_3/documentation/

But how to receive:

host:name/documentation/

If I used this in the template, the correct name would look like this {% url 'app:documentation' %}.


Solution

  • You should use reverse or reverse_lazy.

    documentation = forms.BooleanField(
        label='I accept the terms and <a href="{%s}">conditions</a>.' % reverse_lazy('app:documentation'),
        initial=False
    )
    

    See the docs.