0

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.

1
  • 1
    Sounds like get_main_config is None. Are you sure that main_db.configurations_find_one doesn't ever return None? Commented Jul 26, 2022 at 2:37

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.