Hi Prashant:
Thanks for getting back to me so quickly!
I am using Python 3.7.3 and pymongo 3.10.1.
For my first issue, consider the following:
**client = MongoClient(“mongodb://127.0.0.1:27017”) **
db = client.polymers # get the database
# get the collections
inventories = db.inventory
classes = db.classes
vendors = db.vendors
@app.route("/")
def root():
** # display the list of polymers in the inventory collection i **
** inventory_list = inventories.find()**
** a1 = “active”**
** return render_template(‘index.html’, a1=a1, inventories=inventory_list, t=title, h=heading)**
@app.route("/classes")
def classes():
** # display the list of classes from the classes collection**
** class_list = db.classes.find()
** a2 = "active"
** return render_template(‘classes.html’, a2=a2, classes=class_list, t=title, h=heading)**
Notice that I had to prepend the call to classes.find() with db. I have a similar function for the vendors collection where I had to prepend with db. on the call to vendors.find(). However, I didn’t have to do that with the call to inventories.find(). Could it have something to do with the first connection to a collection in my code is the inventories collection? If I leave out the db. with the calls to classes.find() and vendors.find(), I get the Internal Server Error in the browser and the terminal window error message ends in:
File “app.py”, line 37, in classes
class_list = classes.find()
AttributeError: ‘function’ object has no attribute ‘find’ 1
For my second issue, consider the following:
@app.route("/edit")
def edit():
** polymer_id = request.values.get("_id")**
** print("–> polymer id: “, polymer_id)**
** inventory_edit = inventories.find({”_id": ObjectId(polymer_id)})**
** return render_template(‘edit.html’, inventories=inventory_edit, h=heading, t=title)**
@app.route("/edit-class")
def edit_class():
** class_id = request.values.get("_id")**
** print("–> class id: “, class_id)**
** class_edit = db.classes.find({”_id:": ObjectId(class_id)})**
** return render_template(‘edit-class.html’, classes=class_edit, h=heading, t=title)**
As you can see, the edit() function uses the find() method containing an ObjectId that renders the edit.html file as expected. I am presented with a form to edit the document corresponding to the ObjectId. Awesome!
However, the edit_class() function also uses the find() method containing an ObjectId does not render the edit-class.html file as expected. Nothing crashes, but the form and table within the {% for class in classes %}{% endfor %} is not processed.
Note that I added print statements in the two functions. I get an ObjectId back from each HTML. The generated URL, /edit-class?_id= is also correct, so I suspect there is something in the call to the find() method containing the ObjectId. This works in the MongoDB shell, but not in the app.
I tried find_one() function, but I totally forgot about it returning a document as opposed to a cursor. I believe I got an Internal Server Error with that, but it makes sense now since I was trying to process a result set instead of a document.
I would appreciate any advice! Thanks!
Mike.