Maddie Graham
Maddie Graham

Reputation: 2177

Pass the url view in the forms label. DJango

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' %}.

Any help will be appreciated.

Upvotes: 1

Views: 145

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599470

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.

Upvotes: 2

Related Questions