Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » how to use render_to_response, ajax and javascript variables [RSS Feed]

#1 June 16, 2010 13:20:17

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

how to use render_to_response, ajax and javascript variables


Hi,

Please can anyone help with an app architecture problem I am having?
(I am quite new to Django)

I have an app which which serves up XHR requests (via YUI3 io
uitility) to urlpatterns. The views make HttpResponses using
render_to_response like so:

return render_to_response("registration/register.html", {
'form': form,
}, context_instance=RequestContext(request))

That is all fine: The html content is rendered in the relevant div
(using a YUI3 io's success callback) .

But the problem I have - and I may be thinking about this in the wrong
way - is that I also want to pick out some variables from the response
to use in my js success callback. If I wasn't using django templating
this could be straightforwardly achieved with a JSON response parsed
client side. So my difficulty is that I want both a rendered template
response and some JSON response in the same callback... I have thought
about 'enriching' the render_to_response context with these additional
variables, inserting them in hidden html fields and then querying the
dom for their values - but that feels awkward and also means the
response has to be added to the dom before the js can act on their
values.

This seems like a familiar nut that must be well documented
somewhere... :) any help, pointers very appreciated.

Thanks
Alex

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 June 16, 2010 13:34:13

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

how to use render_to_response, ajax and javascript variables


On Wed, Jun 16, 2010 at 1:17 PM, Alex <alexle...@googlemail.com> wrote:
> Hi,
>
> Please can anyone help with an app architecture problem I am having?
> (I am quite new to Django)
>
> I have an app which which serves up XHR requests (via YUI3 io
> uitility) to urlpatterns.  The views make HttpResponses using
> render_to_response like so:
>
> return render_to_response("registration/register.html", {
>        'form': form,
>    }, context_instance=RequestContext(request))
>
> That is all fine: The html content is rendered in the relevant div
> (using a YUI3 io's success callback) .
>
> But the problem I have - and I may be thinking about this in the wrong
> way - is that I also want to pick out some variables from the response
> to use in my js success callback. If I wasn't using django templating
> this could be straightforwardly achieved with a JSON response parsed
> client side. So my difficulty is that I want both a rendered template
> response and some JSON response in the same callback... I have thought
> about 'enriching' the render_to_response context with these additional
> variables, inserting them in hidden html fields and then querying the
> dom for their values - but that feels awkward and also means the
> response has to be added to the dom before the js can act on their
> values.
>
> This seems like a familiar nut that must be well documented
> somewhere... :) any help, pointers very appreciated.
>
> Thanks
>                  Alex
>

The prototype framework allows you to place (small amounts) of JSON
into the response header 'X-JSON', which it then evaluates, and stores
in transport.headerJSON. If you are using prototype, that could be a
solution, or implement your own version of that.

Cheers

Tom

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 June 16, 2010 13:43:54

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

how to use render_to_response, ajax and javascript variables


Perhaps instead of using render_to_response to generate the response,
render the template output to a string and then stuff that in the data
structure that you serialise to json along with the other data?

Regards,
Matt

On Jun 16, 1:17 pm, Alex <alexle...@googlemail.com> wrote:

>
> But the problem I have - and I may be thinking about this in the wrong
> way - is that I also want to pick out some variables from the response
> to use in my js success callback. If I wasn't using django templating
> this could be straightforwardly achieved with a JSON response parsed
> client side. So my difficulty is that I want both a rendered template
> response and some JSON response in the same callback... I have thought
> about 'enriching' the render_to_response context with these additional
> variables, inserting them in hidden html fields and then querying the
> dom for their values - but that feels awkward and also means the
> response has to be added to the dom before the js can act on their
> values.
>
> This seems like a familiar nut that must be well documented
> somewhere... :) any help, pointers very appreciated.
>
> Thanks
>                   Alex

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 June 16, 2010 16:03:07

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

how to use render_to_response, ajax and javascript variables


Just one comment - Django lets you render to JSON just as easily as
rendering to HTML (or XML). You just write your template as JSON and
set the MIME type for the response accordingly. Because I control my
JSON parsing on the client side, I set the MIME type to text/plain but
you could set to another if you wanted.

I realise that this doesn't actually solve your problem of combining
HTML and JSON in a response and I think one of the other comments
addresses this but I wanted to make the point that JSON by itself is
easy.

Cheers
Ian

On Jun 16, 1:17 pm, Alex <alexle...@googlemail.com> wrote:
> Hi,
>
> Please can anyone help with an app architecture problem I am having?
> (I am quite new to Django)
>
> I have an app which which serves up XHR requests (via YUI3 io
> uitility) to urlpatterns.  The views make HttpResponses using
> render_to_response like so:
>
> return render_to_response("registration/register.html", {
>         'form': form,
>     }, context_instance=RequestContext(request))
>
> That is all fine: The html content is rendered in the relevant div
> (using a YUI3 io's success callback) .
>
> But the problem I have - and I may be thinking about this in the wrong
> way - is that I also want to pick out some variables from the response
> to use in my js success callback. If I wasn't using django templating
> this could be straightforwardly achieved with a JSON response parsed
> client side. So my difficulty is that I want both a rendered template
> response and some JSON response in the same callback... I have thought
> about 'enriching' the render_to_response context with these additional
> variables, inserting them in hidden html fields and then querying the
> dom for their values - but that feels awkward and also means the
> response has to be added to the dom before the js can act on their
> values.
>
> This seems like a familiar nut that must be well documented
> somewhere... :) any help, pointers very appreciated.
>
> Thanks
>                   Alex

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 June 16, 2010 20:46:10

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

how to use render_to_response, ajax and javascript variables


Thanks all. I may go with Matt's idea of serialising html + other data
to json and decoding client side. I'm not totally keen though because
this abandons a very nice rollup of functionality in django's
render_to_response (I am not familiar with how to write the template
as JSON and rendering to that. Keeping the template as html seems like
the right thing to do). I was hoping you'd scream 'don't be daft -
everyone does this...' :)

Alex

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#6 June 17, 2010 08:42:59

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

how to use render_to_response, ajax and javascript variables


Hi Alex, here is a small example of a JSON response. I don't mix HTML
and JSON personally (I use HTML pages and then fetch JSON via AJAX
calls so).

Here is a fragment of code from one of my views:

t = loader.get_template('members/member_info.json')
c = Context({'member_set':member_set})
return HttpResponse(t.render(c), mimetype="text/plain")

Here is the template:

{"results":}

I choose text/plain deliberately but you might choose text/json (or
something else). if you embed HTML in JSON then you will need to be
careful that the HTML does not itself contain characters that break
the JSON (e.g. quotation marks).

Cheers
Ian

On Jun 16, 8:46 pm, Alex <alexle...@googlemail.com> wrote:
> Thanks all. I may go with Matt's idea of serialising html + other data
> to json and decoding client side. I'm not totally keen though because
> this abandons a very nice rollup of functionality in django's
> render_to_response (I am not familiar with how to write the template
> as JSON and rendering to that. Keeping the template as html seems like
> the right thing to do). I was hoping you'd scream 'don't be daft -
> everyone does this...'  :)
>
>                                   Alex

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#7 June 17, 2010 09:02:59

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

how to use render_to_response, ajax and javascript variables


Django has an escapejs filter so if you're using a template to
generate json you'd be safer doing, e.g.:

"first_name":"{{ one_member.first_name|escapejs }}"

That way if someone sticks something untoward (like a double quote) in
your first_name or last_name fields it won't break things :).

For the rolling up of HTML and JSON I personally would use a template
for the HTML but I wouldn't bother with it for the JSON as typically
the stuff you want to chuck back in JSON would be most naturally (I
think) built up as a python data structure (e.g. dict of values) which
you use simplejson (which is included with Django) to convert into
json.

from django.utils import simplejson
returnData={
'success':1,
'htmlData:template.render(context),
'someListData':
}
return HttpResponse(simplejson.dumps(data),mimetype='text/plain)

That way simplejson'll take care of sorting everything out :). If you
want to dump out django objects as json too then look at the
documentation for serialising django objects.

Regards,
Matt

On Jun 17, 8:42 am, Ian McDowall <i.d.mcdow...@gmail.com> wrote:
> Here is the template:
>
> {"results":}
>
> I choose text/plain deliberately but you might choose text/json (or
> something else).  if you embed HTML in JSON then you will need to be
> careful that the HTML does not itself contain characters that break
> the JSON (e.g. quotation marks).

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#8 June 17, 2010 09:18:50

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

how to use render_to_response, ajax and javascript variables


Hi!

Matt Hoskins wrote:
> return HttpResponse(simplejson.dumps(data),mimetype='text/plain)

Small correction: mime type should be application/json.

--
Dmitry Dulepov
Twitter:http://twitter.com/dmitryd/Web:http://dmitry-dulepov.com/--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#9 June 17, 2010 09:37:43

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

how to use render_to_response, ajax and javascript variables


I was just copying Ian's choice of mimetype - see Ian's comment above
"I choose text/plain deliberately but you might choose text/json (or
something else)."... Although it's worth pointing out that "text/json"
shouldn't be used, since "application/json" is, as you rightly point,
the mimetype for json data :).

On Jun 17, 9:18 am, Dmitry Dulepov <dmitry.dule...@gmail.com> wrote:
>
> Small correction: mime type should be application/json.
>

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#10 June 17, 2010 10:31:31

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

how to use render_to_response, ajax and javascript variables


Sorry, other posters have picked up two of my errors.

It is a while since I used application/json and I was running on
(incorrect) memory. My reasoning for using plain text is as follows.

I regard parsing JSON using eval() as a security risk on the client
side. If you have complete control of the server side then it is safe
but I choose to be conservative and safer. I use the json2.js library
to parse JSON rather than using eval() and making the MIME type text
prevents accidental use of eval.

Here are some links about parsing JSON.http://funkatron.com/site/comments/safely-parsing-json-in-javascript/http://yuiblog.com/blog/2007/04/10/json-and-browser-security/With regard to marking fields as safe - yes, Matt Hoskins is right. I
have fixed that in a later version of my template but I didn't have
the latest version to hand (different laptop) so I used an old
version.

In some cases, I don't use templates to build a JSON response. It can
be straightforward to write it as a string inline. I don't personally
yet use the built in Python JSON module as I don't want to limit the
Python versions that I can deploy with but I am sure that I will move
to this at some point.

Cheers
Ian
On Jun 17, 9:37 am, Matt Hoskins <skaffe...@googlemail.com> wrote:
> I was just copying Ian's choice of mimetype - see Ian's comment above
> "I choose text/plain deliberately but you might choose text/json (or
> something else)."... Although it's worth pointing out that "text/json"
> shouldn't be used, since "application/json" is, as you rightly point,
> the mimetype for json data :).
>
> On Jun 17, 9:18 am, Dmitry Dulepov <dmitry.dule...@gmail.com> wrote:
>
>
>
> > Small correction: mime type should be application/json.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » how to use render_to_response, ajax and javascript variables [RSS Feed]

Board footer

Moderator control

Enjoy the 15th 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