Jul-05-2020, 12:42 PM
Hello Coders!
I'm trying to upload data with Python to MongoDB from a form in HTML.
I can do it with no problem if I upload only the form, but I don't know how to add additional values to the same submission, I get errors. I show you my code:
Thank you very much! I'm a beginner then forgive my stupid question.
Thank you!
I'm trying to upload data with Python to MongoDB from a form in HTML.
I can do it with no problem if I upload only the form, but I don't know how to add additional values to the same submission, I get errors. I show you my code:
@app.route('/insert_recipe', methods=['POST']) def insert_recipe(): recipes = mongo.db.recipes x = datetime.datetime.now() posted = x.strftime("%d") + " " + x.strftime("%b") + " " + x.strftime("%Y") date_post = { "posted" : posted } recipes.insert_one(request.form.to_dict(), posted) if request.files: image = request.files["picture"] path = os.getcwd() image_dir = path + "/static/images/" + image.filename image.save(image_dir) # Once that's done, we redirect to recipe.html, so we can view the new recipe in our collection. return redirect(url_for('index'))As you can see request.form.to_dict() is all the data coming from the form, and posted is an additional value that I would like to add. It's important for me to know how to do it, but this doesn't work, I can only manage to upload the form if I leave the code simply with:
recipes = mongo.db.recipes recipes.insert_one(request.form.to_dict())Second thing I would like to rename the image with the Object ID that the submission creates, is this possible?
Thank you very much! I'm a beginner then forgive my stupid question.
Thank you!