My code contains:
Class GroupForm(forms.ModelForm):
....
def clean_name(self):
....
raise forms.ValidationError(mark_safe('....<a href="{% url edit %}">click here</a>.'))
I want to include a url that I need to pass kwargs to in my error message. I’m using mark_safe so that my anchor text characters don’t get escaped.
Currently the url that gets produced on the page is (current url directory)/{% url edit %} . Its just treating my tag as text.
Is there a way I can get this url tag to behave like normal?
Technically, but it’s a tad convoluted.
ValidationErrorexpects plain text, so you’re going to have to give it that one way or another. But you can render the text yourself before feeding it toValidationError:Template.render()requires a context, which is why I just passed in an empty one. But if you have additional variables and such you also need to render, you can pass those intoContextin the form of a dictionary.UPDATE: As Daniel Roseman mentions, this is unnecessary for URLs since you can just use
reverse, but if you need to render any other type of tag, this is the method you need.