将HTML放入Django中的ValidationError中 [英] Put HTML into ValidationError in Django

查看:80
本文介绍了将HTML放入Django中的ValidationError中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在此ValidationError中放置一个锚标记:

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

定位标记为< a href = some / url / to / login>代替登录< / a>

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

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

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

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

预先感谢!

推荐答案

此方法有效:

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顶部也需要文件:

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

from django.utils.safestring import mark_safe

这篇关于将HTML放入Django中的ValidationError中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆