Reputation: 49
I am trying to validate that the parameters in the POST request sent are valid URLs.
This is my views.py
views.py
def post(self, request):
if url_validator(request) == 400:
return Jsonresponse(status=400)
This is my utils.py. This file will contain all general methods and classes.
def url_validator(request, ext):
for key, value in request.data.items():
value = request.data[key]
try:
URLValidator(value)
except ValidationError:
return 400
When I call the function url_validator
from views, it executes but doesn't return the exception when either of the request parameters doesn't contain URLs.
For example, if I pass a parameter param1: "some string"
, it doesn't go through the ValidationError path.
How do I go about getting the correct return from the function?
Upvotes: 0
Views: 685
Reputation: 600059
A validator class doesn't take the input to be validated in its instantiation, it takes it when you call the instantiated object:
validator = URLValidator()
validator(value)
But this really isn't how to do validation in Django. Either use a form, or if you're processing submitted JSON, use a django-rest-framework serializer.
Upvotes: 1