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?
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.
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.
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.
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.
Call mark_safe
on the error message string when you're raising the ValidationError
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With