将HTML放入Django中的ValidationError

时间:2013-09-13 12:53:49

标签: python django

我想在此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')
                )

锚标记为<a href="some/url/to/login">Log in instead</a>

因此,当用户输入已存在的电子邮件时的错误消息将是:

'此电子邮件地址已存在一个帐户。 <a> 登录而不是</a>?'

提前致谢!

1 个答案:

答案 0 :(得分:5)

这有效:

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>')))
 )

...但也需要在Python文件的顶部:

from django.utils.safestring import mark_safe