Django http request to api error

53 views
Skip to first unread message

chern...@gmail.com

unread,
Jan 16, 2018, 11:42:01 PM1/16/18
to Django users

As this is the first time I'm trying this out, I do not know what is wrong with the problem. So it would be great if someone can help me solve this problem


The code I'm using is at the bottom page of this website: https://www.twilio.com/blog/2014/11/build-your-own-pokedex-with-django-mms-and-pokeapi.html

Where it give example on how you can make HTTP request function and retrieve database on your query.


The code on the website is this.

query.py

import requests
import json

BASE_URL = 'http://pokeapi.co'


def query_pokeapi(resource_url):
    url = '{0}{1}'.format(BASE_URL, resource_url)
    response = requests.get(url)

    if response.status_code == 200:
        return json.loads(response.text)
    return None


charizard = query_pokeapi('/api/v1/pokemon/charizard/')

sprite_uri = charizard['sprites'][0]['resource_uri']
description_uri = charizard['descriptions'][0]['resource_uri']

sprite = query_pokeapi(sprite_uri)
description = query_pokeapi(description_uri)

print
charizard['name']
print
description['description']
print
BASE_URL + sprite['image']


In my edit, I only change these print line at the bottom of this

query.py

print(charizard['name'])
print(description['description'])
print(BASE_URL + sprite['image'])


But i got this error instead

Traceback (most recent call last): File "query2.py", line 46, in sprite_uri = charizard['sprites'][0]['resource_uri'] TypeError: 'NoneType' object is not subscriptable

Matemática A3K

unread,
Jan 17, 2018, 1:02:06 AM1/17/18
to django...@googlegroups.com
On Wed, Jan 17, 2018 at 1:42 AM, <chern...@gmail.com> wrote:

As this is the first time I'm trying this out, I do not know what is wrong with the problem. So it would be great if someone can help me solve this problem


The code I'm using is at the bottom page of this website: https://www.twilio.com/blog/2014/11/build-your-own-pokedex-with-django-mms-and-pokeapi.html

Where it give example on how you can make HTTP request function and retrieve database on your query.


The code on the website is this.

query.py

import requests
import json

BASE_URL = 'http://pokeapi.co'


def query_pokeapi(resource_url):
    url = '{0}{1}'.format(BASE_URL, resource_url)
    response = requests.get(url)

    if response.status_code == 200:
        return json.loads(response.text)
    return None
If there is not a 200 response it returns None



charizard = query_pokeapi('/api/v1/pokemon/charizard/')

It's because charizard is None

sprite_uri = charizard['sprites'][0]['resource_uri']
that can't do None['sprites']...

description_uri = charizard['descriptions'][0]['resource_uri']

sprite = query_pokeapi(sprite_uri)
description = query_pokeapi(description_uri)

print
charizard['name']
print
description['description']
print
BASE_URL + sprite['image']


In my edit, I only change these print line at the bottom of this

query.py

print(charizard['name'])
print(description['description'])
print(BASE_URL + sprite['image'])


But i got this error instead

Traceback (most recent call last): File "query2.py", line 46, in sprite_uri = charizard['sprites'][0]['resource_uri'] TypeError: 'NoneType' object is not subscriptable

And that's why the error
 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/af20ad33-1843-4fff-93fb-8681f7d5b88e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

chern...@gmail.com

unread,
Jan 17, 2018, 1:27:05 AM1/17/18
to Django users
May i know what does this means:

def query_pokeapi(resource_url):
    url = '{0}{1}'.format(BASE_URL, resource_url)
    response = requests.get(url)

the '{0}{1}' and resource_url
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Andréas Kühne

unread,
Jan 17, 2018, 3:44:18 AM1/17/18
to django...@googlegroups.com
Hi,

That's called string interpolation. Check here:

Not the best explanation, but should get you going.

Regards,

Andréas

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages