Python Forum
'NoneType' object is not subscriptable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'NoneType' object is not subscriptable
#1
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
This is the error I get
'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 would be greatly 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
Reply


Messages In This Thread
'NoneType' object is not subscriptable - by Justchse - Aug-01-2020, 03:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I am getting this TypeError: 'TreasureMap' object is not subscriptable. makilakos 2 365 May-25-2024, 07:58 PM
Last Post: deanhystad
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 980 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  TypeError: 'NoneType' object is not callable akbarza 4 1,412 Aug-24-2023, 05:14 PM
Last Post: snippsat
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 1,896 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  Help with python 'not subscriptable' error Extra 3 2,457 Dec-16-2022, 05:55 PM
Last Post: woooee
  TypeError: 'NoneType' object is not subscriptable syafiq14 3 5,496 Sep-19-2022, 02:43 PM
Last Post: Larz60+
  AttributeError: 'NoneType' object has no attribute 'group' MaartenRo 3 5,135 Jan-02-2022, 09:53 AM
Last Post: snippsat
  AttributeError: 'NoneType' object has no attribute 'group' MaartenRo 3 2,995 Jan-01-2022, 04:16 PM
Last Post: MaartenRo
  'int' object is not subscriptable after API call ed8484 1 1,941 Sep-18-2021, 02:06 PM
Last Post: ed8484
  Getting 'NoneType' object has no attribute 'find' error when WebScraping with BS Franky77 2 5,527 Aug-17-2021, 05:24 PM
Last Post: Franky77

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020