Returning data from view to ajax call

1,063 views
Skip to first unread message

Larry Martell

unread,
Jul 2, 2013, 6:06:18 PM7/2/13
to django...@googlegroups.com
I'm invoking a view from an ajax call and I want to return data. But
the browser interpertates the return as a 500 (internal sever error)
and my ajax success function does not get called. How can I return a
successful status (like a 201) and also return data?

Sithembewena Lloyd Dube

unread,
Jul 2, 2013, 6:24:42 PM7/2/13
to django...@googlegroups.com
What data format is your view returning? Django views return HTTP response objects (by default, at least) - although Python lists (and other Python collections?) should work when passed into a view in its context (I have found Django querysets to be an exception).

AJAX is Asynchronous Javascript, so I presume that an AJAX call would expect something like JSON output from a callable. I guess with some pain one could get Javascript to ingest Django querysets? I couldn't think of a sensible reason to do so - and I stand to be corrected.



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.





--
Regards,
Sithu Lloyd Dube

Larry Martell

unread,
Jul 2, 2013, 6:37:34 PM7/2/13
to django...@googlegroups.com
On Tue, Jul 2, 2013 at 4:24 PM, Sithembewena Lloyd Dube
<zeb...@gmail.com> wrote:
> What data format is your view returning? Django views return HTTP response
> objects (by default, at least) - although Python lists (and other Python
> collections?) should work when passed into a view in its context (I have
> found Django querysets to be an exception).
>
> AJAX is Asynchronous Javascript, so I presume that an AJAX call would expect
> something like JSON output from a callable. I guess with some pain one could
> get Javascript to ingest Django querysets? I couldn't think of a sensible
> reason to do so - and I stand to be corrected.

I want to return a string. I've never done this before. I've been
reading examples on the web and I came up with this:

$.ajax({
url: 'permalink/',
type: 'GET',
data: {
report: "{% url motor.core.reports.views.view
the_controller.app.name the_controller.get_name %}",
params: "{{ the_report.params_string }}"
},
dataType: 'json',
success: function (data) {
setTimeout(function () {
alert(data);
}, 0);
}
});

In my function I first tried returning just the string, but that was
causing a 500 error. Then I tried this:

return HttpResponse(content=data, content_type="text/plain", status=201)

I don't get the error, but the ajax success function does not seem to be called.

Sanjay Bhangar

unread,
Jul 2, 2013, 6:49:15 PM7/2/13
to django...@googlegroups.com
You shouldn't need the setTimeout in your success call - just function(data) { alert(data); } .. should be okay.

In my function I first tried returning just the string, but that was
causing a 500 error. Then I tried this:

return HttpResponse(content=data, content_type="text/plain", status=201)

try status=200 ?
 
I don't get the error, but the ajax success function does not seem to be called.

An invaluable tool here is to use the Developer Tools in the browser to inspect AJAX requests made and their responses .. in Chrome, open Developer Tools (f12 is a handy shortcut for this) and go to the Network Tab, and then you should see the Network Requests show up, and be able to examine what was returned, etc..

Hope that helps,
Sanjay

Larry Martell

unread,
Jul 2, 2013, 7:01:34 PM7/2/13
to django...@googlegroups.com
In the browser the response has this:

HTTP/1.0 200 OK
Date: Tue, 02 Jul 2013 22:53:47 GMT
Server: WSGIServer/0.1 Python/2.7.2
Vary: Cookie
Content-Type: text/plain

No content. But I verified from pdb that the python code is sending
something in the content when it does this:

return HttpResponse(content=data, content_type="text/plain", status=200)

Sanjay Bhangar

unread,
Jul 2, 2013, 7:14:07 PM7/2/13
to django...@googlegroups.com
On Tue, Jul 2, 2013 at 4:01 PM, Larry Martell <larry....@gmail.com> wrote:
<snip /> 
In the browser the response has this:

HTTP/1.0 200 OK
Date: Tue, 02 Jul 2013 22:53:47 GMT
Server: WSGIServer/0.1 Python/2.7.2
Vary: Cookie
Content-Type: text/plain

No content. But I verified from pdb that the python code is sending
something in the content when it does this:

return HttpResponse(content=data, content_type="text/plain", status=200)

try:
  return HttpResponse(data, content_type="text/plain", status=200)
 
(no content= ... )

best've luck,
Sanjay

Larry Martell

unread,
Jul 2, 2013, 7:20:38 PM7/2/13
to django...@googlegroups.com
No difference - still no content.

Sanjay Bhangar

unread,
Jul 2, 2013, 7:24:34 PM7/2/13
to django...@googlegroups.com
Can you navigate to the page just in the browser (not making an AJAX request) ? Do you get a response then? What if you substitute data with "some random string" ?

-Sanjay


Larry Martell

unread,
Jul 2, 2013, 7:39:25 PM7/2/13
to django...@googlegroups.com
Yes, I get back the data I expect.

Larry Martell

unread,
Jul 3, 2013, 9:14:06 AM7/3/13
to django...@googlegroups.com
I just tried setting an always function and that is called and I can
get my response. So I guess I'll go with that. But I wonder why the
success function is not called.

C. Kirby

unread,
Jul 3, 2013, 11:30:19 AM7/3/13
to django...@googlegroups.com
An HttpResponse includes http headers and will blow up an ajax call.
You want to use a render_to_string method and pass either json or a rendered template (render to string bypasses the headers, render to response will include them)

Larry Martell

unread,
Jul 3, 2013, 11:47:38 AM7/3/13
to django...@googlegroups.com
On Wed, Jul 3, 2013 at 9:30 AM, C. Kirby <mis...@gmail.com> wrote:
> An HttpResponse includes http headers and will blow up an ajax call.
> You want to use a render_to_string method and pass either json or a rendered
> template (render to string bypasses the headers, render to response will
> include them)

I don't want to return a template, just some data. I tried this:

return render_to_string(simplejson.dumps(data))

But it blows up with template not found.

C. Kirby

unread,
Jul 3, 2013, 12:01:26 PM7/3/13
to django...@googlegroups.com
I'm sorry, I was typing without thinking.
render_to_string() if you want to use a template without headers
render_to_response() includes headers

HttpResponse(json.dumps(data_object)) should be what you want.

It would also be helpful to see the code for your view. It looks like you already put the template code concerning the ajax call in this thread.

Larry Martell

unread,
Jul 3, 2013, 1:00:50 PM7/3/13
to django...@googlegroups.com
On Wed, Jul 3, 2013 at 10:01 AM, C. Kirby <mis...@gmail.com> wrote:
> I'm sorry, I was typing without thinking.
> render_to_string() if you want to use a template without headers
> render_to_response() includes headers
>
> HttpResponse(json.dumps(data_object)) should be what you want.
>
> It would also be helpful to see the code for your view. It looks like you
> already put the template code concerning the ajax call in this thread.

The view is very simple. It does some stuff and ends up with a single
short string that I am returning like this:

return HttpResponse(data, content_type="application/json", status=200)

I can pick up the string in an always function from data.responseText
so I'm going to go with that, declare victory and move on. Thanks to
everyone for the help.
Reply all
Reply to author
Forward
0 new messages