Home > other >  Terminate an endpoint from within a function Django
Terminate an endpoint from within a function Django

Time:06-14

Is there a way to end an endpoint from inside a function? I know I can make the function to return True or False, then evaluate that answer and return, but it would be a lot cleaner if I can do something like this:

def custom_endpoint(self, request):
    check_something(request)
    return Response("ok")

check_something(request) should check variables and then end the execution of the endpoint if one of the variables is not correct.

Thanks!!

CodePudding user response:

Your utility function can only return to its caller, i.e. your view function. You must somehow use the return value of your utility function to end the execution of your view function. Your view function must return a web response.

Of course, you can also use an error-handling mechanism instead of using return values in case of errors.

This is also discussed here.

  • Related