This is my function in views.py which calls the api and send the response on html page.

   r = requests.post(RUN_URL, data=data)
   return JsonResponse(r.json(), safe=False)

I have a global variable 'counter' which I want to return along with that r.json().

   return JsonResponse(r.json(),counter, safe=False)

When I tried this code it gives me a error:

  TypeError: 'int' object is not callable
1

There are 1 best solutions below

4
On

You can create a dictionary where you return both the data and the counter, like:

return JsonResponse({'data': r.json(), 'counter': counter})

I have a global variable 'counter'.

Please do not use global variables. Global variables introduce a global state, which makes testing, refactoring, and distributing software very painful.