Reputation:
I am playing around with GeoIP2 and requested the following in my view.
g = GeoIP2()
city = g.city('google.com')
tests = Test.objects.all()
args = { 'tests': tests }
return render(request, 'app/home.html', args)
I receive a JSON response with a bunch of data, I am interested in "city" for example.
{'city': None, 'continent_code': 'NA', 'continent_name': 'North America', 'country_code': 'US', 'country_name': 'United States', 'dma_code': None, 'latitude': 37.751, 'longitude': -97.822, 'postal_code': None, 'region': None, 'time_zone': 'America/Chicago'}
My model
# Create your models here.
class Test(models.Model):
city = models.CharField(default='', max_length=100)
def __str__(self):
return self.browser_family
Despite some googling and Youtube videos I am not quite sure how I should grab for example "city" out of the JSON response. I looked at previous threads here but not quite sure it can be applied here, seems like other threads were for more sophisticated stuff.
Any suggestions out there?
SOLVED city = g.city('google.com') json = city['city']
Upvotes: 1
Views: 70
Reputation: 2623
You can grab/assign it as follows:
city = JSON_response(‘city‘)
What happens here:
You assign the value
of the key
"city" of your JSON_reponse
to the variable
"city.
In your example city
will be None
Upvotes: 1