Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 March 25, 2008 00:20:06

U.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to return this kind of JSON?


I've been using this more or less this approach:

PYTHON:

t = loader.get_template('posts.html')
html = t.render(Context({ 'tweets' : tweets }))

data = { 'html': html, 'tweet_count': len(tweets) }
json = simplejson.dumps(data)

return HttpResponse(json, mimetype = 'application/javascript')

JAVASCRIPT, in your xhr callback fn:

var jsonObject = eval('(' + jsonString + ')');
var html = jsonObject.html; //now you can inject this into a
container
var listLength = jsonObject.tweet_count; //you can use the extra meta-
data too

Hope that helps,
Udi



On Mar 22, 3:43 pm, truebosko <> wrote:
> Hi there,
>
> I am trying to switch my function to return a JSON request. Right now,
> it simply returns an HTML template, here's the basic code:
>
> return render_to_response('posts.html',
> { 'tweets' : tweets })
>
> Simple, obviously. Tweets is just a list, and posts.html goes through
> them (Using a for loop) and out pops my template, with 1 .. * tweets
> listed.
>
> But, I want to change it around, I want to do something like the
> following:
>
> return:
> - posts.html formatted like it currently is
> - Several variables, not within the HTML
>
> I looked around and I simply can't figure out how to do it. Any
> suggestions?
>
> Thanks
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#2 March 25, 2008 00:42:49

t.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to return this kind of JSON?


Thanks guys, and Udi for the example. That shed some light on what I
needed to do and it worked great!



On Mar 24, 7:19 pm, Udi <> wrote:
> I've been using this more or less this approach:
>
> PYTHON:
>
> t = loader.get_template('posts.html')
> html = t.render(Context({ 'tweets' : tweets }))
>
> data = { 'html': html, 'tweet_count': len(tweets) }
> json = simplejson.dumps(data)
>
> return HttpResponse(json, mimetype = 'application/javascript')
>
> JAVASCRIPT, in your xhr callback fn:
>
> var jsonObject = eval('(' + jsonString + ')');
> var html = jsonObject.html; //now you can inject this into a
> container
> var listLength = jsonObject.tweet_count; //you can use the extra meta-
> data too
>
> Hope that helps,
> Udi
>
> On Mar 22, 3:43 pm, truebosko <> wrote:
>
> > Hi there,
>
> > I am trying to switch my function to return a JSON request. Right now,
> > it simply returns an HTML template, here's the basic code:
>
> > return render_to_response('posts.html',
> > { 'tweets' : tweets })
>
> > Simple, obviously. Tweets is just a list, and posts.html goes through
> > them (Using a for loop) and out pops my template, with 1 .. * tweets
> > listed.
>
> > But, I want to change it around, I want to do something like the
> > following:
>
> > return:
> > - posts.html formatted like it currently is
> > - Several variables, not within the HTML
>
> > I looked around and I simply can't figure out how to do it. Any
> > suggestions?
>
> > Thanks
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

Board footer

Moderator control

Enjoy the 16th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support