Django view.py - Unable to render it through api genrated json file

Hello,
I am trying to display sunrise from the below dict file but I would get the value: 1618358103 only but i need to display sunrise instead. please review the below code along with Django views.py file.

=================views.py=============================

def homeView(request):
views = {}
city = “Kathmandu”
url = f"http://api.openweathermap.org/data/2.5/weather?q={city} &appid=dd799068b7ecc1b7446b9588d15e7e0e"
data = requests.get(url).json()
# print(data)
payload = {‘city’: data[‘name’],
‘weather’ : data[‘weather’][0][‘main’],
‘icon’: data[‘weather’][0][‘icon’],
‘kelvin_temperature’: data[‘main’][‘temp’],
‘celcius_temperature’: data[‘main’][‘temp’] - 273,
‘pressure’: data[‘main’][‘pressure’],
‘humidity’: data[‘main’][‘humidity’],
‘sunrise’: data[‘sys’] [‘sunrise’]

}
my_dict2 = {'data': payload}
date = datetime.datetime.now()
date = date.strftime("%c")
my_dict = {'date_msg': date}
views['navbar'] = navbar.objects.all()
return render(request, 'index.html', context={'date_msg': date, 'data': payload})

=====================index.html===================================

  • {{data.celcius_temperature}}ºc,Sunny{{data.humidity}} {**{data.sunrise}}**
  • {{date_msg}}

output: 1618358103 instead i need to print sunrise which is key.

Please post a sample of the JSON data you’re receiving from that API.

(Also, when you’re posting code, templates, or data here, please enclose it between two lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This ensures that the code remains formatted correctly.)

1 Like

Actually, I just recognized this as a Unix Epoch time - translates to April 13 @ 11:55:03 PM UTC. See the Python datetime module for options to convert this to a localized timestamp value.

1 Like

JSON file
{"coord":{"lon":85.3167,"lat":27.7167},"weather":[{"id":721,"main":"Haze","description":"haze","icon":"50n"}],"base":"stations","main":{"temp":292.15,"feels_like":291.54,"temp_min":292.15,"temp_max":292.15,"pressure":1018,"humidity":55},"visibility":5000,"wind":{"speed":1.54,"deg":120},"clouds":{"all":20},"dt":1618419342,"sys":{"type":1,"id":9201,"country":"NP","sunrise":1618358103,"sunset":1618404155},"timezone":20700,"id":1283240,"name":"Kathmandu","cod":200}

The schema definition for this data is provided in the api docs. You can find everything there you need to convert these values to human-readable forms.