How to return a JSON response in Django


from django.shortcuts import render
from django.http import JsonResponse

# Create your views here.
def index(request):
return render(request,'file_upload/index.html')

def send_json(request):
# data = {'id': 4, 'name': 'Test Response','roles' : ['Admin','User']}
data = [
{'id':'101', 'first_name':'Ram','last_name':'Pukar'},
{'id':'102', 'first_name':'Mukesh','last_name':'Thapa'},
{'id':'103', 'first_name':'Anil','last_name':'Shah','email_id':['anil@hotmail.com','anil_info@hotmail.com','anil_test@hotmail.com']},
{'id':'104', 'first_name':'Dinesh','last_name':'Yadav'},
{'id':'105', 'first_name':'Hira','last_name':'Shakya'}
]
return JsonResponse(data,safe=False)

Output:

Share on Google Plus

About Ram Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment