How to Solve Error Message global name ‘JsonResponse’ is not defined

Posted on

In order to solve the error happened because of the error message triggered as shown in the title of the article, basically a simple solution can be done to solve it. The error message is triggered upon executing a page which is made or developed based on Django framework. The error message which is ‘global name ‘JsonResponse’ is not defined is triggered upon using the ‘JsonResponse’ in the script. The script which is mentioned is part of application built in Django framework. In the context of this article, the error message itself is declared in a file named as shown below :

def save(request, form, template_name)
  data = dict()
  return JsonReponse(data)

The above is a snippet code or part of the script which is written in the file named in the form of a function named save. Based on the function defined, it is going to return data which is going to be formatted into a JSON format. But actually that is not the problem, the main problem is as soon as the function being executed the error is triggered. The solution is quite simple for solving the problem.

In this case, the solution is just to find out whether JsonResponse can be imported from the available library provided. The library is actually exist in the library provided by Django framework. Just import the ‘JsonResponse’ by adding the following line in the file where the definition for using ‘JsonResponse’ exist as shown below :

from django.http import JsonResponse

Based on the line above defined in the file, the error message will be resolved quickly. So, the error message itself where there is a part which is containing “global name ‘string’ is not defined, basically the ‘string’ part doesn’t recognized by Python interpreter. The string itself is not recognized as a reserved keyword for python programming language nor part of the library or class imported. So, in order to resolve it, one way or another is by searching the name whether it is actually exist as part of Django library.

Leave a Reply