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.
CodePudding user response:
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.
CodePudding user response:
It means that get_main_config
is of type None
and None
is not a subscriptable type. For an object to be subscriptable in Python it must support indexing (lists, tuples, strings etc. are subscriptable). Here, since get_main_config
is None
(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 return None
is 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.