Aug-01-2020, 03:02 PM
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 = TrueThis 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 +0000Traceback, 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