可以在ValidationError的字符串中使用模板标记吗?

2024-05-12 23:02:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要抛出包含锚的ValidationError。你知道吗

if not profile.activated():
    raise ValidationError('Your profile is not activated. <a href="{% url resend_activation_key %}">Resend activation key</a>.')

我需要修改什么才能工作?你知道吗


Tags: keyurlyourifisnotprofileactivation
2条回答

为什么要在这里使用模板标记?模板标记用于模板中。如果要查找反向URL,请使用^{}函数。你知道吗

第一:别这样!将HTML代码放在模板中。你知道吗

第二:你也许可以这样做

from django.template import Context, Template
t = Template(u"Your profile is not.... {% url blah %} ...")
raise ValidationError( t.render(Context())

但是html标记将被转义,除非你在模板中将它们标记为安全的。你知道吗

相关问题 更多 >