Asking for some help on this one.
def home(request):
client = pymongo.MongoClient(settings.MONGO_SERVER)
main_db = client[settings.MONGO_DATABASE]
get_main_config = main_db.configurations.find_one({"name": "main_config"})
return render(request, 'dashboard/home.html', {"data": get_main_config["homepage_urls"]})
Traceback (most recent call last):
render(request, 'dashboard/home.html', {"data": get_main_config["homepage_urls"]})
TypeError: 'NoneType' object is not subscriptable
Why the error occured on that line?
Thank you.
2
Answers
The error shows up when you use operator [] on a variable whose value is None. So it can only be
get_main_config
. Just print the variable to check.It means that
get_main_config
is of typeNone
andNone
is not a subscriptable type. For an object to be subscriptable in Python it must support indexing (lists, tuples, strings etc. are subscriptable). Here, sinceget_main_config
isNone
(which does not support indexing) and you are trying to access the key'homepage_urls'
from it you end up with the error you got.The
find_one
method you are using is designed to find a single document matching your query or returnNone
if it could not find any. (https://pymongo.readthedocs.io/en/stable/tutorial.html#getting-a-single-document-with-find-one)So, you may want to make sure that a document matching the query
{'name': 'main_config'}
actually exists in your database.