1👍
✅
Here can be used a list comprehension:
def lookup_code(request):
locode_url = 'https://pkgstore.datahub.io/core/un-locode/code-list_json/data/05f6ccfe0cd03ab51bed07273b982df9/code-list_json.json'
try:
city = request.GET.get('city')
r = requests.get(locode_url)
raw_data = (json.loads(r.content))
site_codes = [
{
"city_code": res['Location'],
"country_code": res['Country'],
"state": res['Subdivision'],
"site_code": "%s-%s" % (res['Country'],res['Location']),
}
for res in raw_data if res['Name'] == city
]
return render(request, 'app_sitecode/home.html', {'site_codes': site_codes})
except:
return render(request, 'app_sitecode/home.html', {'cleartext':'', 'city':'INVALID CITY'})
template:
{% for site in site_codes %}
{{ site.site_code }}, {{ site.state }}
{% endfor %}
👤NKSM
Source:stackexchange.com