Reputation: 423
I have an error message in forms.py
to pass it to a template, but the template is rendering as a string not a html tag.
In forms.py
message = ("The email address is already taken. Please <a href='#'> log in. </a>"
def clean_email(self):
email = self.cleaned_data['email']
try:
User.objects.get_by_natural_key(email)
except User.DoesNotExist:
return email
raise forms.ValidationError(message)
In a template
<span class="error">{{form.email.errors|safe}}</span>
But this is rendering as 'The email address is already taken. Please <a href='#'>
log in.</a>
' not '
The email address is already taken. Please log in.'
I tried the autoescape tag, {% autoescape off%}{{form.email.errors|safe}}{% endautoescape%}
, but this doesn't work either.
What am I missing? Thanks in advance.
Upvotes: 2
Views: 1067
Reputation: 1081
You should try to:
raise forms.ValidationError(mark_safe(message))
See the mark_safe documentation for more information
For building up fragments of HTML, you should normally be using django.utils.html.format_html() instead
Upvotes: 3