Python Forum
filtering by category flask+mongodb - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: filtering by category flask+mongodb (/thread-28422.html)



filtering by category flask+mongodb - Leon79 - Jul-18-2020

Hi coders! I'm dividing every page by filtering by the correct category in my recipe blog, but I don't know why I cannot manage. For example, I have vegetarian.html, It works when I simply don't look for a specific category in MongoDB, but when I do I get an error, I write here the code:

This is main.py

@app.route('/vegetarian')
def vegetarian():
    query={{"category_name": "Vegetarian"}}
    return render_template("vegetarian.html", recipes=mongo.db.recipes.find(query))
The error is this:

File "/Users/RBianco/Desktop/Course restart/cookbook-milestone/main.py", line 35, in vegetarian
query={{"category_name": "Vegetarian"}}
TypeError: unhashable type: 'dict'


For sure I wrote it wrong.

Any help please?

Thank you guys!!!


RE: filtering by category flask+mongodb - ndc85430 - Jul-18-2020

Did you mean to put your dict inside a set on line 3?


RE: filtering by category flask+mongodb - Leon79 - Jul-18-2020

(Jul-18-2020, 05:18 PM)ndc85430 Wrote: Did you mean to put your dict inside a set on line 3?

Thanks man, I just wrote query={{"category_name": "Vegetarian"}} that actually had to be query={"category_name": "Vegetarian"}

I just got confused with the {{}}

:)


RE: filtering by category flask+mongodb - ndc85430 - Jul-19-2020

Also, FWIW, the error happens because dictionaries are mutable. If you were to change a mutable item, its hash value would change meaning that it could be in the wrong bucket in the underlying hash table. That would mean looking up the item would be broken. Dictionary keys have to be immutable things for the same reason.