You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

18 lines
476 B
Python

from django.shortcuts import render
from django.http import JsonResponse
import requests
# Create your views here.
def get_client_ip(request):
client_ip = request.META.get('REMOTE_ADDR', '')
try:
response = requests.get(f'http://ipinfo.io/{client_ip}/json')
data = response.json()
country = data.get('country', 'Unknown')
except Exception as e:
country = "Unknown"
return JsonResponse({'ip': client_ip, 'country': country})