django - Django:フォームのclean()メソッドからdjango検証エラーにハイパーリンクを配置するにはどうすればよいですか?
4971 次
2 に答える
42
mark_safe
あなたが発生しているときにエラーメッセージ文字列を呼び出しますValidationError
于 2009-02-17T15:17:58.860 に答える
11
次のように、フォーム レベルの ValidationError を発生させる必要なく、フォーム フィールド定義で実行できます。
class RegistrationForm(ModelForm):
...
### Django established methods
# form wide cleaning/validation
def clean_email(self):
""" prevent users from having same emails """
email = self.cleaned_data["email"]
try:
User.objects.get(email__iexact=email)
raise forms.ValidationError(
mark_safe(('A user with that email already exists, click this <a href="{0}">Password Reset</a> link'
' to recover your account.').format(urlresolvers.reverse('PasswordResetView')))
)
except User.DoesNotExist:
return email
...
### Additional fields
location = forms.RegexField(max_length=255,
regex=r"^[\w' -]+, [\w'-]+, [\w'-]+, [\w'-]+$", #ex 1 Mclure St, Kingston, Ontario, Canada
help_text="location, ex: Suite 212 - 1 Main St, Toronto, Ontario, Canada",
error_messages={
'invalid': mark_safe("Input format: <strong>suite - street</strong>, <strong>city</strong>, "
"<strong>province/state</strong>, <strong><u>country</u></strong>. Only letters, "
"numbers, and '-' allowed.")})
于 2013-07-09T20:26:50.817 に答える