How to send Dictionary object(converted JSON file) to template using render in DJANGO

Submitted 4 years, 5 months ago
Ticket #267
Views 541
Language/Framework Django
Priority Low
Status Closed

I am fetching an external json file . Whenever I send the data in dictionary form to html page and reload it, The page doesn't opens instead that html file is downloaded.

def index(request):
   data = urllib.request.urlopen("https://some/states_daily.json").read()

   output = json.loads(data)

   print(output)
 
   return render(request,'index.html',{'countries':countries},{'states':output})

Can anyone help me on this issue?

Submitted on Oct 27, 20
add a comment

1 Answer

Verified

You can try something like this,

def index(request):
    data = urllib.request.urlopen(
        'https://some/states_daily.json'
    ).read()
    output = json.loads(data)
    countries = 'India'
    context ={}
    context['countries'] = countries
    context['states'] = output
    return render(
        request,
        'india.html', context
    )

Submitted 4 years, 5 months ago


Latest Blogs