I want to get a response of get function present inside class-based view. So I have written a small function outside the scope of a class. How to pass a URL to this get function or how to call this function directly in order to get its response? The function will fetch the value from a template and need to pass the fetched paramter value to get function inside Get_data. The Get_data class currently serves purpose of to directly get values from URL called and give json response and I want to extend its functionality where by I want I get value from template and pass it to this Get_data function and get its response.
Sample code:
class Get_data(APIView):
def get(self, request):
username = request.GET.get('username')
# Processing Code #
response_data = {'status': 'SUCCESS',
'data': XYZ}
return Response(response_data, status=status.HTTP_200_OK)
def fetch_value():
# Code to get response data from GET function of class Get_data by passing GET parameter value
#
So what should be the code inside the function fetch_value to pass the value fetched from a template and get response data from GET function of class Get_data?
fetch_value()is supposed to achieve, i.e. how you're going to use that function.fetch_value()function and how are you going to use it. Unless you're trying to write tests for your view (in which case there are existing tools), the proper solution is very probably to extract the code you're interested in from yourget()method into a distinct function or method that you can call from both points.