Just a little correction to tutorial "Write a Tumblelog Application with Flask and MongoEngine"

1,113 views
Skip to first unread message

maikel

unread,
Jun 17, 2012, 3:16:39 PM6/17/12
to mongodb-user
My congratulations for the author of the above mentioned tutorial!. As
a newcomer to MongoDB and Flask, this has been excellent to me to
learn and practice some features and capabilities of both.

Just a little couple of corrections in order other readers can avoid
some drawbacks I've met. In last section named "Converting the Blog to
a Tumblelog":

-In "models.py" listing says:

class Post(db.Dynamic Document):
created_at = db.DateTimeField(default=datetime.datetime.now,
required=True)
title = db.StringField(max_length=255, required=True)
slug = db.StringField(max_length=255, required=True)
comments = db.ListField(db.EmbeddedDocumentField('Comment'))

and shoud say:

class Post(db.Document):
created_at = db.DateTimeField(default=datetime.datetime.now,
required=True)
title = db.StringField(max_length=255, required=True)
slug = db.StringField(max_length=255, required=True)
body = db.StringField(required=True)
comments = db.ListField(db.EmbeddedDocumentField('Comment'))

Best regards!

Ross Lawley

unread,
Jun 18, 2012, 4:55:30 AM6/18/12
to mongod...@googlegroups.com
Hi Maikel,

It converts to a dynamic document and the BlogPost model has the body.   In the tutorial the other types don't include or require the body.  However, you can easily add fields and update the models to meet your criteria.

All the best,

Ross

maikel

unread,
Jun 18, 2012, 6:05:58 PM6/18/12
to mongodb-user
Hi Ross,

Thanks for your quick answer. I think I understand what you mean, ...
but not completely. Now I've used "class Post(db.DynamicDocument)" -
not "class Post(db.Dynamic Document)"- and without "body = ..."
sentence, but I have some warnings in Flask console and when editing
any of the Post I got an error: AttributeError: 'QuerySet' object has
no attribute 'get_or_404'

WARNINGS:

(venv) C:\Users\Miquel\python\miblog\tumblelog>python manage.py
runserver
C:\Users\Miquel\python\miblog\venv\lib\site-packages\mongoengine
\base.py:558: FutureWarning: BlogPost uses inheritance, the default
for allow_inheritance is hanging to off by default. Please add it to
the document meta.
FutureWarning
C:\Users\Miquel\python\miblog\venv\lib\site-packages\mongoengine
\base.py:558: FutureWarning: Video uses inheritance, the default for
allow_inheritance is hanging to off by default. Please add it to the
document meta.
FutureWarning
C:\Users\Miquel\python\miblog\venv\lib\site-packages\mongoengine
\base.py:558: FutureWarning: Image uses inheritance, the default for
allow_inheritance is hanging to off by default. Please add it to the
document meta.
FutureWarning
C:\Users\Miquel\python\miblog\venv\lib\site-packages\mongoengine
\base.py:558: FutureWarning: Quote uses inheritance, the default for
allow_inheritance is hanging to off by default. Please add it to the
document meta.
FutureWarning
* Running on http://0.0.0.0:5000/
* Restarting with reloader
C:\Users\Miquel\python\miblog\venv\lib\site-packages\mongoengine
\base.py:558: FutureWarning: BlogPost uses inheritance, the default
for allow_inheritance is changing to off by default. Please add it to
the document meta.
FutureWarning
C:\Users\Miquel\python\miblog\venv\lib\site-packages\mongoengine
\base.py:558: FutureWarning: Video uses inheritance, the default for
allow_inheritance is changing to off by default. Please add it to the
document meta.
FutureWarning
C:\Users\Miquel\python\miblog\venv\lib\site-packages\mongoengine
\base.py:558: FutureWarning: Image uses inheritance, the default for
allow_inheritance is changing to off by default. Please add it to the
document meta.
FutureWarning
C:\Users\Miquel\python\miblog\venv\lib\site-packages\mongoengine
\base.py:558: FutureWarning: Quote uses inheritance, the default for
allow_inheritance is changing to off by default. Please add it to the
document meta.
FutureWarning

ERRORS:

127.0.0.1 - - [18/Jun/2012 23:52:32] "GET /admin/post-imatge/ HTTP/
1.1" 500 -
Traceback (most recent call last):
File "C:\Users\Miquel\python\miblog\venv\lib\site-packages\flask
\app.py", line 1518, in __call__ return self.wsgi_app(environ,
start_response)
File "C:\Users\Miquel\python\miblog\venv\lib\site-packages\flask
\app.py", line 1506, in wsgi_app response =
self.make_response(self.handle_exception(e))
File "C:\Users\Miquel\python\miblog\venv\lib\site-packages\flask
\app.py", line 1504, in wsgi_app response =
self.full_dispatch_request()
File "C:\Users\Miquel\python\miblog\venv\lib\site-packages\flask
\app.py", line 1264, in full_dispatch_request rv =
self.handle_user_exception(e)
File "C:\Users\Miquel\python\miblog\venv\lib\site-packages\flask
\app.py", line 1262, in full_dispatch_request rv =
self.dispatch_request()
File "C:\Users\Miquel\python\miblog\venv\lib\site-packages\flask
\app.py", line 1248, in dispatch_request return
self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Miquel\python\miblog\tumblelog\auth.py", line 26, in
decorated return f(*args, **kwargs)
File "C:\Users\Miquel\python\miblog\venv\lib\site-packages\flask
\views.py", line 84, in view return self.dispatch_request(*args,
**kwargs)
File "C:\Users\Miquel\python\miblog\venv\lib\site-packages\flask
\views.py", line 151, in dispatch_request return meth(*args, **kwargs)
File "C:\Users\Miquel\python\miblog\tumblelog\admin.py", line 57, in
get context = self.get_context(slug)
File "C:\Users\Miquel\python\miblog\tumblelog\admin.py", line 35, in
get_context post = Post.objects.get_or_404(slug=slug)
AttributeError: 'QuerySet' object has no attribute 'get_or_404'
127.0.0.1 - - [18/Jun/2012 23:52:32] "GET /admin/post-imatge/?
__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [18/Jun/2012 23:52:32] "GET /admin/post-imatge/?
__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [18/Jun/2012 23:52:32] "GET /admin/post-imatge/?
__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [18/Jun/2012 23:52:32] "GET /admin/post-imatge/?
__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 -
127.0.0.1 - - [18/Jun/2012 23:52:32] "GET /admin/post-imatge/?
__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [18/Jun/2012 23:52:32] "GET /admin/post-imatge/?
__debugger__=yes&cmd=resource&f=source.png HTTP/1.1" 200 -

Probably I'm doing something wrong, but I'm still not skilled enough
to find it out. I'm afraid I'll have to study the code in more
detail...

Regards,
Miquel

Ross Lawley

unread,
Jun 29, 2012, 5:30:37 AM6/29/12
to mongod...@googlegroups.com
Hi,

Miquel, have you got any further with this?  get_or_404 should be patched onto the queryset by the flask-mongoengine package.  Have you got the latest package?  Its now on pypi.

Ross
Message has been deleted
Message has been deleted

maikel

unread,
Jul 2, 2012, 8:21:26 AM7/2/12
to mongodb-user
Hi Ross,

Thanks for your interest.

Well, I've done some advance, I hope. I begin to understand what you
mean with DynamicDocument, it's really powerful!

In order to avoid WARNING messages I've added in module "models.py"
and in class Post "'allow_inheritance': True" in document meta. This
works.

But I've still had the error: "AttributeError: 'QuerySet' object has
no attribute 'get_or_404'" .. and then I followed your last
suggestion. I've reinstalled lask_mongoengine" package with: "pip
install https://github.com/rozza/flask-mongoengine/tarball/master"
( ... btw I think this is what I did the first time)

I've investigated this error changing calls to
"get_or_403" (inexisting method) instead of "get_or_404", and the
results are different if using "class Post(db.Document)" or "class
Post(db.Dynamic Document)"

using db.Document:
File "C:\Users\Miquel\python\miblog\tumblelog\views.py", line 23,
in get_context
post = Post.objects.get_or_403(slug=slug)
AttributeError: 'BaseQuerySet' object has no attribute
'get_or_403'

using db.DynamicDocument:
File "C:\Users\Miquel\python\miblog\tumblelog\views.py", line 23,
in get_context
post = Post.objects.get_or_403(slug=slug)
AttributeError: 'QuerySet' object has no attribute 'get_or_403'

Does his add some clue?

My temporal workaround to make this working has been substituting
"get_or_404(...)" with "get(...)" but I'm afraid this can lead to
problems in case some page does not exists.

Thanks again. Regards,
Miquel
> >  * Running onhttp://0.0.0.0:5000/

maikel

unread,
Jul 5, 2012, 3:07:19 PM7/5/12
to mongodb-user
Hello,

I've tried all combinations into my possibilities. I've used both
installations:

...
pip install https://github.com/hmarr/mongoengine/tarball/dev
pip install https://github.com/rozza/flask-mongoengine/tarball/master

and

...
pip install mongoengine
pip install flask-mongoengine

(in this last case imports are like

"from flask.ext.mongoengine import MongoEngine" and "from
flask.ext.mongoengine.wtf import model_form" instead of
"from flaskext.mongoengine import MongoEngine" and "from
flaskext.mongoengine.wtf import model_form"

because of python directories alre slightly different)

I've done this both in Windows 7 and Ubuntu Desktop. And the results
are the same. Well, in case of "pip install mongoengine", when using
admin.py there is a new error:

"""
File "/home/miquel/miblog-pip/venv/lib/python2.7/site-packages/
flask_wtf/form.py", line 69, in __init__
[Display the sourcecode for this frame] [Open an interactive python
shell in this frame] raise Exception('Must provide secret_key to use
csrf.')
Exception: Must provide secret_key to use csrf.
"""

I'm very interested in building up a personal tumblelog based this
example because I think it could be improved and become very powerful
and versatile.

But so far I don't know what else I can try.

Thanks. Regards,
Miquel

On 2 jul, 14:21, maikel <miquel....@gmail.com> wrote:
> Hi Ross,
>
> Thanks for your interest.
>
> Well, I've done some advance, I hope. I begin to understand what you
> mean with DynamicDocument, it's really powerful!
>
> In order to avoid WARNING messages I've added in module "models.py"
> and in class Post "'allow_inheritance': True" in document meta. This
> works.
>
> But I've still had the error: "AttributeError: 'QuerySet' object has
> no attribute 'get_or_404'" .. and then I followed your last
> suggestion. I've reinstalled lask_mongoengine" package with: "pip
> installhttps://github.com/rozza/flask-mongoengine/tarball/master"
> > onto the queryset by the flask-mongoenginepackage.  Have you got the

Ross Lawley

unread,
Jul 6, 2012, 4:33:32 AM7/6/12
to mongod...@googlegroups.com
Hi,

Sorry to hear your having such trouble, seems that the docs need some updating to handle the latest version of Flask.  I'll submit a patch request to update the docs and I need to update MongoEngine as there has been a regression with inherited documents and custom querysets.

I'm returning from europython to the UK and will let you know once this has been updated and fixed.

Once again apologies for the frustration - bare with me.

Ross

Ross Lawley

unread,
Jul 11, 2012, 5:26:00 AM7/11/12
to mongod...@googlegroups.com
Hi Maikel,

I've fixed the issue in flask-mongoengine and updated the tutorial documentation.   You should now have no problems, you will need to uninstall flask-mongoengine and then reinstall, but its now on pypi so pip install flask-mongoengine will work.

All the best,

Ross

On Sunday, 17 June 2012 20:16:39 UTC+1, maikel wrote:

maikel

unread,
Jul 16, 2012, 5:20:39 AM7/16/12
to mongod...@googlegroups.com
Hi Ross,

Good news! Now it's working fine!

Just to comment a minimal think I had to add due to my ignorance on Flask, I had to add the sentence "app.secret_key = ..." in __init__.py in order to avoid error "Exception: Must provide secret_key to use csrf.", which I think is quite usual in Flask

Thanks again to your gentle intro to the power of Flask + MongoDB + MongoEngine!! Now I feel encouraged to start a more solid learning on these technologies and make some deployments.

All the best,
Miquel


maikel

unread,
Jul 16, 2012, 5:27:15 AM7/16/12
to mongod...@googlegroups.com
Oh! I see that you had already included my comment in your doc review ... ;)

Asif Akhtar

unread,
Apr 17, 2016, 7:21:38 PM4/17/16
to mongodb-user, mique...@gmail.com
i want to add delete button to comments. how i can do that
Reply all
Reply to author
Forward
0 new messages