Monday, April 25, 2011

Re: URLs in error messages from form validation

On Apr 25, 2011, at 5:39 AM, Kane Dou wrote:

> * Daniel Gerzo <dgerzo@gmail.com> [2011-04-25 13:07:46 +0200]:
>
>> On 25.4.2011 12:40, Kane Dou wrote:
>>> * Daniel Gerzo<dgerzo@gmail.com> [2011-04-24 17:52:13 +0200]:
>>>
>>>> Hello all,
>>>>
>>>> say I have a following form:
>>>>
>>>> class MyForm(forms.Form):
>>>> id = forms.IntegerField(max_length=9)
>>>> language = forms.CharField(max_length=10)
>>>> file = forms.FileField()
>>>>
>>>> And a following pseudo clean method:
>>>>
>>>> def clean(self):
>>>> for file in self.files.values():
>>>> if file_exists(file):
>>>> raise forms.ValidationError('We already have this
>>>> file<a href="/somehwere">here</a>')
>>>> return self.cleaned_data

>>>>
>>>> So I want to display a URL in the error message. The text is
>>>> displayed fine, but the URL is being escaped by Django and thus is
>>>> not clickable. Is there some way to disable it for this specific
>>>> case?

You only need to mark the string as safe as in:

http://docs.djangoproject.com/en/1.3/ref/utils/#module-django.utils.safestring

from django.utils.safestring import mark_safe

raise forms.ValidationError(mark_safe('We already have this
file<a href="/somehwere">here</a>'))

Jason

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate