Re: how to use render_to_response, ajax and javascript variables

2010-06-17 Thread Alex
Thanks all! I will try a combination of these pointers. server/client communication patterns seems to be a recurring theme (or nightmare!) of mine. This conversation has been very helpful and reassuring - I can see that the hard and fast rule is 'when in doubt, json it'

Re: how to use render_to_response, ajax and javascript variables

2010-06-17 Thread Matt Hoskins
On Jun 17, 10:31 am, Ian McDowall wrote: > > 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

Re: how to use render_to_response, ajax and javascript variables

2010-06-17 Thread Ian McDowall
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 th

Re: how to use render_to_response, ajax and javascript variables

2010-06-17 Thread Matt Hoskins
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 js

Re: how to use render_to_response, ajax and javascript variables

2010-06-17 Thread Dmitry Dulepov
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

Re: how to use render_to_response, ajax and javascript variables

2010-06-17 Thread Matt Hoskins
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 th

Re: how to use render_to_response, ajax and javascript variables

2010-06-17 Thread Ian McDowall
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})

Re: how to use render_to_response, ajax and javascript variables

2010-06-16 Thread Alex
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 th

Re: how to use render_to_response, ajax and javascript variables

2010-06-16 Thread Ian McDowall
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 ano

Re: how to use render_to_response, ajax and javascript variables

2010-06-16 Thread Matt Hoskins
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 wrote: > > But the problem I have - and I may be th

Re: how to use render_to_response, ajax and javascript variables

2010-06-16 Thread Tom Evans
On Wed, Jun 16, 2010 at 1:17 PM, Alex 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_res

how to use render_to_response, ajax and javascript variables

2010-06-16 Thread Alex
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/regi