Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: How can I put an <a> hyperlink in a django validation error from a forms clean() method?

Django: How can I put an <a> hyperlink in a django validation error from a forms clean() method? I want to raise a validation error, and in the error text have an <a href=""></a> hyperlink that has a link that would help the user correct the error. This is a validation error I'm raising in a clean method of a form. Is there a way to mark the HTML for that validation error as safe to output as HTML?

like image 680
MikeN Avatar asked Feb 17 '09 15:02

MikeN


People also ask

How do I display validation error in Django?

To display the form errors, you use form. is_valid() to make sure that it passes validation. Django says the following for custom validations: Note that any errors raised by your Form.

What is form Is_valid () in Django?

The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.

How do I extend a validation error in Django?

The to_python() method on a Field is the first step in every validation. It coerces the value to a correct datatype and raises ValidationError if that is not possible. This method accepts the raw value from the widget and returns the converted value.

What method can you use to check if form data has changed when using a form instance?

Use the has_changed() method on your Form when you need to check if the form data has been changed from the initial data. has_changed() will be True if the data from request.


1 Answers

Call mark_safe on the error message string when you're raising the ValidationError

like image 165
Jonny Buchanan Avatar answered Oct 07 '22 12:10

Jonny Buchanan