Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put HTML into ValidationError in Django

Tags:

python

django

I want to put an anchor tag into this ValidationError:

Customer.objects.get(email=value)
            if self.register:
                # this address is already registered
                raise forms.ValidationError(
                    _('An account already exists for this email address')
                )

The anchor tag would be <a href="some/url/to/login">Log in instead</a>

So, the error message when a user inputs an email that already exists would be something like:

'An account already exists for this email address. <a>Log in instead</a>?'

Thanks in advance!

like image 359
David Ingledow Avatar asked Sep 13 '13 12:09

David Ingledow


1 Answers

This worked:

Customer.objects.get(email=value)
    if self.register:
    # this address is already registered
    raise forms.ValidationError(
    (_(mark_safe('An account already exists for this email address. <a href="#" class="email_error">Log in instead?</a>')))
 )

...but also needed this at the top of the Python file:

from django.utils.safestring import mark_safe
like image 132
David Ingledow Avatar answered Nov 06 '22 02:11

David Ingledow