librelist archives

« back to archive

how to return specific field values from a mongodb document rather than a template

how to return specific field values from a mongodb document rather than a template

From:
Andrew Gordon
Date:
2012-07-27 @ 23:26
I posted here a week or so ago about getting Ajax working, which I ended 
up getting to work, basically.

However, now I am trying to return an updated mongodb document's field 
value so that it can be loaded into a <span>, but I don't understand how 
to return only specific mongodb content rather then reloading an entire 
template (and thus negating the beauty of AJAX in not having to reload a 
whole page. The ideal workflow is that each record displayed in the 
browser has a vote for it, when a user clicks the "vote for me" link in 
the browser, to vote for that record, it sends an increment back to the 
server (which works) and then updates the vote tally in the browser 
(which does not work).

Python route: https://pastee.org/jr528
HTML + JS: https://pastee.org/erd7w
an example record: https://pastee.org/nuanz

Currently the ajax works to have a 'test' string implanted into the span 
when i click the link. But i would like to see the vote incremented in 
the browser with AJAX, and that's where I am having this problem; I am 
not sure how to return the specific updated record back to the client so 
that it can be loaded into the '.vote_tally' <span>

I am using pymongo for dealing with the mongo collection within flask.

Re: [flask] how to return specific field values from a mongodb document rather than a template

From:
Robert Moskal
Date:
2012-07-28 @ 04:28
Just have the voteup endpoint return the current count as a json object.
Use a jquery getJson call to get the return result and insert it in the DOM
where you like.

Best.

______________
Robert Moskal
Most Media
Brooklyn, USA
On Jul 27, 2012 7:29 PM, "Andrew Gordon" <drew.s.gordon@gmail.com> wrote:

>  I posted here a week or so ago about getting Ajax working, which I ended
> up getting to work, basically.
>
> However, now I am trying to return an updated mongodb document's field
> value so that it can be loaded into a <span>, but I don't understand how to
> return only specific mongodb content rather then reloading an entire
> template (and thus negating the beauty of AJAX in not having to reload a
> whole page. The ideal workflow is that each record displayed in the browser
> has a vote for it, when a user clicks the "vote for me" link in the
> browser, to vote for that record, it sends an increment back to the server
> (which works) and then updates the vote tally in the browser (which does
> not work).
>
> Python route: https://pastee.org/jr528
> HTML + JS: https://pastee.org/erd7w
> an example record: https://pastee.org/nuanz
>
> Currently the ajax works to have a 'test' string implanted into the span
> when i click the link. But i would like to see the vote incremented in the
> browser with AJAX, and that's where I am having this problem; I am not sure
> how to return the specific updated record back to the client so that it can
> be loaded into the '.vote_tally' <span>
>
> I am using pymongo for dealing with the mongo collection within flask.
>
>

Re: [flask] how to return specific field values from a mongodb document rather than a template

From:
Andrew Gordon
Date:
2012-07-28 @ 13:49
Is jsonify the best way to do that on the flask side? I tried to use
jsonify with the pymongo .find() query, but was getting error about cursors.
On Jul 27, 2012 11:35 PM, "Robert Moskal" <rmoskal@mostmedia.com> wrote:

> Just have the voteup endpoint return the current count as a json object.
> Use a jquery getJson call to get the return result and insert it in the DOM
> where you like.
>
> Best.
>
> ______________
> Robert Moskal
> Most Media
> Brooklyn, USA
> On Jul 27, 2012 7:29 PM, "Andrew Gordon" <drew.s.gordon@gmail.com> wrote:
>
>>  I posted here a week or so ago about getting Ajax working, which I ended
>> up getting to work, basically.
>>
>> However, now I am trying to return an updated mongodb document's field
>> value so that it can be loaded into a <span>, but I don't understand how to
>> return only specific mongodb content rather then reloading an entire
>> template (and thus negating the beauty of AJAX in not having to reload a
>> whole page. The ideal workflow is that each record displayed in the browser
>> has a vote for it, when a user clicks the "vote for me" link in the
>> browser, to vote for that record, it sends an increment back to the server
>> (which works) and then updates the vote tally in the browser (which does
>> not work).
>>
>> Python route: https://pastee.org/jr528
>> HTML + JS: https://pastee.org/erd7w
>> an example record: https://pastee.org/nuanz
>>
>> Currently the ajax works to have a 'test' string implanted into the span
>> when i click the link. But i would like to see the vote incremented in the
>> browser with AJAX, and that's where I am having this problem; I am not sure
>> how to return the specific updated record back to the client so that it can
>> be loaded into the '.vote_tally' <span>
>>
>> I am using pymongo for dealing with the mongo collection within flask.
>>
>>

Re: [flask] how to return specific field values from a mongodb document rather than a template

From:
Smartboy
Date:
2012-07-29 @ 17:00
Unfortunately you can't use jsonify, you have to use json.dumps. MongoDB
uses custom objects in bson.json_util to serialize Non-json-native BSON
objects to JSON and back. Check the docs on bson.json_util. :)

On Sat, Jul 28, 2012 at 6:49 AM, Andrew Gordon <drew.s.gordon@gmail.com>wrote:

> Is jsonify the best way to do that on the flask side? I tried to use
> jsonify with the pymongo .find() query, but was getting error about cursors.
> On Jul 27, 2012 11:35 PM, "Robert Moskal" <rmoskal@mostmedia.com> wrote:
>
>> Just have the voteup endpoint return the current count as a json object.
>> Use a jquery getJson call to get the return result and insert it in the DOM
>> where you like.
>>
>> Best.
>>
>> ______________
>> Robert Moskal
>> Most Media
>> Brooklyn, USA
>> On Jul 27, 2012 7:29 PM, "Andrew Gordon" <drew.s.gordon@gmail.com> wrote:
>>
>>>  I posted here a week or so ago about getting Ajax working, which I
>>> ended up getting to work, basically.
>>>
>>> However, now I am trying to return an updated mongodb document's field
>>> value so that it can be loaded into a <span>, but I don't understand how to
>>> return only specific mongodb content rather then reloading an entire
>>> template (and thus negating the beauty of AJAX in not having to reload a
>>> whole page. The ideal workflow is that each record displayed in the browser
>>> has a vote for it, when a user clicks the "vote for me" link in the
>>> browser, to vote for that record, it sends an increment back to the server
>>> (which works) and then updates the vote tally in the browser (which does
>>> not work).
>>>
>>> Python route: https://pastee.org/jr528
>>> HTML + JS: https://pastee.org/erd7w
>>> an example record: https://pastee.org/nuanz
>>>
>>> Currently the ajax works to have a 'test' string implanted into the span
>>> when i click the link. But i would like to see the vote incremented in the
>>> browser with AJAX, and that's where I am having this problem; I am not sure
>>> how to return the specific updated record back to the client so that it can
>>> be loaded into the '.vote_tally' <span>
>>>
>>> I am using pymongo for dealing with the mongo collection within flask.
>>>
>>>

Re: [flask] how to return specific field values from a mongodb document rather than a template

From:
Andrew Gordon
Date:
2012-07-30 @ 02:43
thank you, that definitely helps.

On 7/29/12 12:00 PM, Smartboy wrote:
> Unfortunately you can't use jsonify, you have to use json.dumps. MongoDB
> uses custom objects in bson.json_util to serialize Non-json-native BSON
> objects to JSON and back. Check the docs on bson.json_util. :)
>
> On Sat, Jul 28, 2012 at 6:49 AM, Andrew Gordon <drew.s.gordon@gmail.com
> <mailto:drew.s.gordon@gmail.com>> wrote:
>
>     Is jsonify the best way to do that on the flask side? I tried to use
>     jsonify with the pymongo .find() query, but was getting error about
>     cursors.
>
>     On Jul 27, 2012 11:35 PM, "Robert Moskal" <rmoskal@mostmedia.com
>     <mailto:rmoskal@mostmedia.com>> wrote:
>
>         Just have the voteup endpoint return the current count as a json
>         object. Use a jquery getJson call to get the return result and
>         insert it in the DOM where you like.
>
>         Best.
>
>         ______________
>         Robert Moskal
>         Most Media
>         Brooklyn, USA
>
>         On Jul 27, 2012 7:29 PM, "Andrew Gordon"
>         <drew.s.gordon@gmail.com <mailto:drew.s.gordon@gmail.com>> wrote:
>
>             I posted here a week or so ago about getting Ajax working,
>             which I ended up getting to work, basically.
>
>             However, now I am trying to return an updated mongodb
>             document's field value so that it can be loaded into a
>             <span>, but I don't understand how to return only specific
>             mongodb content rather then reloading an entire template
>             (and thus negating the beauty of AJAX in not having to
>             reload a whole page. The ideal workflow is that each record
>             displayed in the browser has a vote for it, when a user
>             clicks the "vote for me" link in the browser, to vote for
>             that record, it sends an increment back to the server (which
>             works) and then updates the vote tally in the browser (which
>             does not work).
>
>             Python route: https://pastee.org/jr528
>             HTML + JS: https://pastee.org/erd7w
>             an example record: https://pastee.org/nuanz
>
>             Currently the ajax works to have a 'test' string implanted
>             into the span when i click the link. But i would like to see
>             the vote incremented in the browser with AJAX, and that's
>             where I am having this problem; I am not sure how to return
>             the specific updated record back to the client so that it
>             can be loaded into the '.vote_tally' <span>
>
>             I am using pymongo for dealing with the mongo collection
>             within flask.
>
>

Re: [flask] how to return specific field values from a mongodb document rather than a template

From:
Rhys Elsmore
Date:
2012-07-28 @ 14:13
The jsonify function requires a dictionary to be passed to it. Chances are
that you were trying to pass it the pymongo object which it can't serialize.

Cheers,
Rhys

On 29/07/2012, at 12:03 AM, Andrew Gordon <drew.s.gordon@gmail.com> wrote:

Is jsonify the best way to do that on the flask side? I tried to use
jsonify with the pymongo .find() query, but was getting error about cursors.
On Jul 27, 2012 11:35 PM, "Robert Moskal" <rmoskal@mostmedia.com> wrote:

> Just have the voteup endpoint return the current count as a json object.
> Use a jquery getJson call to get the return result and insert it in the DOM
> where you like.
>
> Best.
>
> ______________
> Robert Moskal
> Most Media
> Brooklyn, USA
> On Jul 27, 2012 7:29 PM, "Andrew Gordon" <drew.s.gordon@gmail.com> wrote:
>
>>  I posted here a week or so ago about getting Ajax working, which I ended
>> up getting to work, basically.
>>
>> However, now I am trying to return an updated mongodb document's field
>> value so that it can be loaded into a <span>, but I don't understand how to
>> return only specific mongodb content rather then reloading an entire
>> template (and thus negating the beauty of AJAX in not having to reload a
>> whole page. The ideal workflow is that each record displayed in the browser
>> has a vote for it, when a user clicks the "vote for me" link in the
>> browser, to vote for that record, it sends an increment back to the server
>> (which works) and then updates the vote tally in the browser (which does
>> not work).
>>
>> Python route: https://pastee.org/jr528
>> HTML + JS: https://pastee.org/erd7w
>> an example record: https://pastee.org/nuanz
>>
>> Currently the ajax works to have a 'test' string implanted into the span
>> when i click the link. But i would like to see the vote incremented in the
>> browser with AJAX, and that's where I am having this problem; I am not sure
>> how to return the specific updated record back to the client so that it can
>> be loaded into the '.vote_tally' <span>
>>
>> I am using pymongo for dealing with the mongo collection within flask.
>>
>>

Re: [flask] how to return specific field values from a mongodb document rather than a template

From:
Marshel Helsper
Date:
2012-07-28 @ 02:26
Andrew,

Could you possibly have a second AJAX call to get the updated vote count?
Or just increment the vote count in Javascript without any call to the
server.

On Fri, Jul 27, 2012 at 7:26 PM, Andrew Gordon <drew.s.gordon@gmail.com>wrote:

>  I posted here a week or so ago about getting Ajax working, which I ended
> up getting to work, basically.
>
> However, now I am trying to return an updated mongodb document's field
> value so that it can be loaded into a <span>, but I don't understand how to
> return only specific mongodb content rather then reloading an entire
> template (and thus negating the beauty of AJAX in not having to reload a
> whole page. The ideal workflow is that each record displayed in the browser
> has a vote for it, when a user clicks the "vote for me" link in the
> browser, to vote for that record, it sends an increment back to the server
> (which works) and then updates the vote tally in the browser (which does
> not work).
>
> Python route: https://pastee.org/jr528
> HTML + JS: https://pastee.org/erd7w
> an example record: https://pastee.org/nuanz
>
> Currently the ajax works to have a 'test' string implanted into the span
> when i click the link. But i would like to see the vote incremented in the
> browser with AJAX, and that's where I am having this problem; I am not sure
> how to return the specific updated record back to the client so that it can
> be loaded into the '.vote_tally' <span>
>
> I am using pymongo for dealing with the mongo collection within flask.
>
>


-- 
Thanks,

Marshel

"They that can give up essential liberty to obtain a little temporary
safety deserve neither liberty nor safety." - Ben Franklin

Re: [flask] how to return specific field values from a mongodb document rather than a template

From:
Andrew Gordon
Date:
2012-07-28 @ 13:47
I was hoping to keep it on the python side, trying to get to know flask
better and all. Seems like it would be more complicated to increment w the
javascript when I assume flask should be able to go back in and pull the
new value out of the db. Thanks however.
 On Jul 27, 2012 9:29 PM, "Marshel Helsper" <marsol0x@gmail.com> wrote:

> Andrew,
>
> Could you possibly have a second AJAX call to get the updated vote count?
> Or just increment the vote count in Javascript without any call to the
> server.
>
> On Fri, Jul 27, 2012 at 7:26 PM, Andrew Gordon <drew.s.gordon@gmail.com>wrote:
>
>>  I posted here a week or so ago about getting Ajax working, which I ended
>> up getting to work, basically.
>>
>> However, now I am trying to return an updated mongodb document's field
>> value so that it can be loaded into a <span>, but I don't understand how to
>> return only specific mongodb content rather then reloading an entire
>> template (and thus negating the beauty of AJAX in not having to reload a
>> whole page. The ideal workflow is that each record displayed in the browser
>> has a vote for it, when a user clicks the "vote for me" link in the
>> browser, to vote for that record, it sends an increment back to the server
>> (which works) and then updates the vote tally in the browser (which does
>> not work).
>>
>> Python route: https://pastee.org/jr528
>> HTML + JS: https://pastee.org/erd7w
>> an example record: https://pastee.org/nuanz
>>
>> Currently the ajax works to have a 'test' string implanted into the span
>> when i click the link. But i would like to see the vote incremented in the
>> browser with AJAX, and that's where I am having this problem; I am not sure
>> how to return the specific updated record back to the client so that it can
>> be loaded into the '.vote_tally' <span>
>>
>> I am using pymongo for dealing with the mongo collection within flask.
>>
>>
>
>
> --
> Thanks,
>
> Marshel
>
> "They that can give up essential liberty to obtain a little temporary
> safety deserve neither liberty nor safety." - Ben Franklin
>