In my django app, this is my validator.py
from django.core.exceptions import ValidationError
from django.core.validators import URLValidatordef validate_url(value):url_validator = URLValidator()url_invalid = Falsetry:url_validator(value)except:url_invalid = Truetry:value = "http://"+valueurl_validator(value)url_invalid = Falseexcept:url_invalid = Trueif url_invalid:raise ValidationError("Invalid Data for this field")return value
which is used to validate this :
from django import forms
from .validators import validate_url
class SubmitUrlForm(forms.Form):url = forms.CharField(label="Submit URL",validators=[validate_url])
When I enter URL like google.co.in
, and print the value right before returning it from validate_url, it prints http://google.co.in
but when I try to get the cleaned_data['url']
in my views, it still shows google.co.in
. So where does the value returned by my validator go and do I need to explicitly edit the clean() functions to change the url field value??
The doc says the following:
The clean() method on a Field subclass is responsible for running to_python(), validate(), and run_validators() in the correct order and propagating their errors. If, at any time, any of the methods raise ValidationError, the validation stops and that error is raised. This method returns the clean data, which is then inserted into the cleaned_data dictionary of the form.
I am still not sure where the validator return value goes and if it is possible to change cleaned_data dict using the validator.