C
C
carrallton2020-10-09 18:35:36
API
carrallton, 2020-10-09 18:35:36

Open Weather API Map - How to make in Celsius?

Hello, I want to add a widget to a website in Django using the Open Weather API Map.

views.py

from django.shortcuts import render
import requests
 
def weather(request):
    url = 'http://api.openweathermap.org/data/2.5/weather?q={}&units=CODEAPI'
    city = 'Kaliningrad'
    city_weather = requests.get(url.format(city)).json()
    weather = {
        'city': city,
        'temperature': city_weather['main']['temp'],
        'description': city_weather['weather'][0]['description'],
        'icon': city_weather['weather'][0]['icon']
    }
    context = {'weather': weather}
    return render(request, 'admin/weather.html', context)


Reading the documentation, I did not find a way how it is possible to give temperature data in Celsius on the output, and not in Fahrenheit. Has anyone had experience of how this can be done?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DevMan, 2020-10-09
@carrallton

&units=metric

A
abberdeen, 2020-12-26
@abberdeen

Alternatively, you can convert, by default the API returns the temperature value in Kelvin, so:

#Конвертируем Кельвин в Цельсию
float(temperature) - 273.15

More on how to convert server side:
https://openweathermap.org/api/one-call-api#data

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question