getting error 'NoneType' object is not subscriptable

I’m writing an API for a Django application that gets inventory from another website. The code was working at first but recently I get the error “NoneType object is not Subscriptable”. I’m new to this but I understand why NoneType is not Subscriptable. I just can’t figure out how to resolve it in my code.

    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3)'
                      ' AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'}

    login_url = 'http://www.website.com/login/validate_login'
    inventory_url = 'http://www.website.com:1002/orders/t_lookup'
    t_search_url = 'http://www.website.com:1002/orders/do_t_lookup?update=t_list'
    t_fitment_url = 'http://www.website.com:1002/fit/update_fit'

    login_params = {
        "commit": "Login",
        "login[account]": os.getenv("tsearch_number"),
        "login[password]": os.getenv("tsearch_number"),
        "login[sales]": "1400"
    }

    t_search_params = {
        "authenticity_token": None,
        "commit": "Find",
        "search_options[exact]": 1,
        "search_options[max]": 300,
        "search_options[scol]": 1,
        "search_options[show_cost]":
            {0: 0,
             1: 1},
        "search_options[show_retail]": 0,
        "search_options[size]": None,
        "search_options[zero]": 0,
        "utf8": True
    }



    cookie_dir = "extras/"
    cookie_file = cookie_dir + "session.cookie"

    logged_in = False
    session = requests.Session()
    session.headers = headers
    if os.path.exists(cookie_file):
        print("Found existing cookies. Loading into session.")
        with open(cookie_file, "rb") as f:
            cookies = requests.utils.cookiejar_from_dict(pickle.load(f))
        session.cookies.update(cookies)
        logged_in = True

    if not os.path.exists(cookie_dir):
        print("Extras folder not found. Creating.")
        os.mkdir(cookie_dir)

    if not logged_in:
        print("Not signed into website. Signing in.")
        session.post(login_url, data=login_params, headers=headers)
        with open(cookie_file, "wb") as f:
            pickle.dump(session.cookies.get_dict(), f)

    s = session.get(inventory_url)

    b = bs4(s.text, "html5lib")
    auth = b.find(attrs={"name": "authenticity_token"})["Value"]
    token = t_search_params["authenticity_token"]
    if "login" in s.url:
        GlobalSettings.search_enabled = False
        raise Exception("Login failed")
    else:
        GlobalSettings.search_enabled = True
    ```
Error message from django

    'NoneType' object is not subscriptable
    Request Method: GET
    Request URL:    https://testsite.com/
    Django Version: 2.2.6
    Exception Type: TypeError
    Exception Value:    
    'NoneType' object is not subscriptable
    Exception Location: /home/website/projectdir/website/t_search.py in <module>, line 68
    Python Executable:  /home/testsite/projectdir/djangoprojectenv/bin/python
    Python Version: 3.6.9
    Python Path:    
    ['/home/testsite/projectdir',
     '/home/testsite/projectdir/djangoprojectenv/bin',
     '/usr/lib/python36.zip',
     '/usr/lib/python3.6',
     '/usr/lib/python3.6/lib-dynload',
     '/home/testsite/projectdir/djangoprojectenv/lib/python3.6/site-packages']
    Server time:    Sat, 1 Aug 2020 03:55:21 +0000

Traceback, any help will be appreciated

    File "/home/testsite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
      34.             response = get_response(request)

    File "/home/testsite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
      100.         resolver_match = resolver.resolve(request.path_info)

    File "/home/testsite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/urls/resolvers.py" in resolve
      536.             for pattern in self.url_patterns:

    File "/home/testsite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/utils/functional.py" in __get__
      80.         res = instance.__dict__[self.name] = self.func(instance)

    File "/home/testsite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/urls/resolvers.py" in url_patterns
      584.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)

    File "/home/testsite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/utils/functional.py" in __get__
      80.         res = instance.__dict__[self.name] = self.func(instance)

    File "/home/testsite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/urls/resolvers.py" in urlconf_module
      577.                 return import_module(self.urlconf_name)

    File "/usr/lib/python3.6/importlib/__init__.py" in import_module
      126.     return _bootstrap._gcd_import(name[level:], package, level)

    File "<frozen importlib._bootstrap>" in _gcd_import
      994. <source code not available>

    File "<frozen importlib._bootstrap>" in _find_and_load
      971. <source code not available>

    File "<frozen importlib._bootstrap>" in _find_and_load_unlocked
      955. <source code not available>

    File "<frozen importlib._bootstrap>" in _load_unlocked
      665. <source code not available>

    File "<frozen importlib._bootstrap_external>" in exec_module
      678. <source code not available>

    File "<frozen importlib._bootstrap>" in _call_with_frames_removed
      219. <source code not available>

    File "/home/testsite/projectdir/testsites/urls.py" in <module>
      22.     url(r'^', include('website.urls'), name='website'),

    File "/home/testsite/projectdir/djangoprojectenv/lib/python3.6/site-packages/django/urls/conf.py" in include
      34.         urlconf_module = import_module(urlconf_module)

    File "/usr/lib/python3.6/importlib/__init__.py" in import_module
      126.     return _bootstrap._gcd_import(name[level:], package, level)

    File "<frozen importlib._bootstrap>" in _gcd_import
      994. <source code not available>

    File "<frozen importlib._bootstrap>" in _find_and_load
      971. <source code not available>

    File "<frozen importlib._bootstrap>" in _find_and_load_unlocked
      955. <source code not available>

    File "<frozen importlib._bootstrap>" in _load_unlocked
      665. <source code not available>

    File "<frozen importlib._bootstrap_external>" in exec_module
      678. <source code not available>

    File "<frozen importlib._bootstrap>" in _call_with_frames_removed
      219. <source code not available>

    File "/home/testsite/djangoprojectdir/website/urls.py" in <module>
      7. from . import views

    File "/home/testsite/djangoprojectdir/website/views.py" in <module>
      9. import website.tire_search as ts

    File "/home/testsite/djangoprojectdir/website/t_search.py" in <module>
      68. auth = b.find(attrs={"name": "authenticity_token"})["value"]

    Exception Type: TypeError at /
    Exception Value: 'NoneType' object is not subscriptable

So the bottom of the traceback is showing this line:

This implies to me that the b.find statement isn’t finding anything (returning None), of which you’re trying to subscript by ‘[“Value”]’ - which doesn’t make any sense, hence the error message.

Either you’re not getting the data you’re expecting to receive, or it’s not coming in the form you’re expecting to see it in, but that’s the basic problem.

Yep the code won’t work the way its written but I don’t know how to resolve it.

So you’ve got a couple choices - you can either:

  • find out what it is you’re supposed to be looking for in the data being retrieved and fix your find call
  • split the find call to a different line, check what you get from it, then only perform the indexing if you’ve got results
  • wrap this statement in a try / except block to catch the error and handle it as necessary

I’m guessing what you really want to do is the first item, but doing that is going to require more information than what’s provided here.

What I like to do in these situations is use the Django shell to perform the first couple of steps manually, and then just start poking around with it to see what I’m getting.

Ken

I was thinking that maybe the class attribute has a different name and/or the html-code has been changed. The code was originally done in Django version2.6. I’ve tried the first option so maybe I’ll look into your other options.