2

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?

3
  • There i no request object here? Commented Feb 6, 2020 at 11:44
  • 1
    This sounds like a XY problem (asking the wrong question), why would you want to do this in the first place? Explain at a higher level what fetch_value() is supposed to achieve, i.e. how you're going to use that function. Commented Feb 6, 2020 at 11:46
  • Please explain WHY you want to do this - why are you writing this 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 your get() method into a distinct function or method that you can call from both points. Commented Feb 6, 2020 at 12:09

2 Answers 2

2

You can use the .as_view() and then "dispatch" the request:

def fetch_value(request):
    response = Get_data.as_view()(request)
    # …

This will return a HttpResponse object [Django-doc] that you can then inspect, for example its .content attribute [Django-doc].

That being said, why don't you just add the Get_data.as_view() result in the urls.py? Processing a response is often not a good idea, since then a lot of "context" is already lost.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use the below code:

from rest_framework.request import HttpRequest, Request


def fetch_value():
    obj = Get_data()
    # make http request object
    http_request_obj = HttpRequest()
    # make http request object
    request = Request(http_request_obj)
    query_output_data = obj.get(request)

1 Comment

how can I pass a URL to request object so that I get its repsonse?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.