I am learning Flask framework and creating a blog website using MongoDB as my back end db. I am perfectly able to save my data into a collection as a document, but I have trouble retrieving a saved document from my collection.
I am using an Azure Cosmos DB MongoDB API for my collection. Please find the necessary screenshots below.
Document Class Definition
class UserDocument(Document, UserMixin):
username = StringField(min_length=2,max_length=100,required=True,unique=True)
email = EmailField(required=True,unique=True)
password = StringField(max_length=60,required=True,unique=True)
remember = BooleanField(required=True,default=True,unique=True)
meta = { 'collection' : 'UserDocument'}
Register route - Saving my document
@app.route("/register",methods= ["POST","GET"] )
def register():
form = RegistrationForm()
if form.validate_on_submit():
if request.method == "POST":
user = UserDocument()
user.username = form.username.data
user.email = form.email.data
user.password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
user.save()
flash(form.password.data,"Info")
return render_template('register.html',form=form,title='Register')
MongoEngine documentation - for retrieving
Issue in retrieving document
@app.route("/login",methods= ["POST","GET"] )
def login():
form = LoginForm()
if form.validate_on_submit():
if request.method == "POST":
# table = client.db.MongoEngineDB
# user = table.UserDocument.find_one({'email':form.email.data})
user = UserDocument.objects(username=form.email.data)
flash(user, "Info")
return render_template('login.html',form=form,title='Login')
I am not sure where I am messing up, I did spend a lot of time trying to figure out myself by going through lot of contents but unable to find out the root cause. Not sure if this has to do with any missing modules or the "UserDocument" class definition.
Not much information available in the "flask-mongoengine" documentation as well.