user776942
user776942

Reputation:

To json or not to json

New to Django and Python.

I am using MySQL as a backend. I have two views: an infinite scroll call that calls all the records in tableA and an autocomplete field that queries tableB and returns matching records from a column.

My infinite scroll and autocomplete were created using help from various separate tutorials around the web.

In my infinite scroll, I am currently returning a render_to_response object (I based it off the Django beginner's tutorial). My autocomplete returns simplejson (I based it off some articles I googled).

They both are returning records from a DB, so shouldn't the responses be similar? When should I use json (or simplejson, in my case) and when shouldn't I? Thx!

Upvotes: 1

Views: 158

Answers (1)

TimD
TimD

Reputation: 1381

It depends entirely on what you're trying to do. render_to_response passes some data to a template to render an HTML document. simply responding with a JSON object will return a JSON object. If you want to present a usable page to a human, then use render_to_response. If you're simply passing some data to a jQuery element, then simply returning a simplejson.dumps() is perfectly valid. There are other ways to return JSON, but that's by far the easiest and most robust.

In order to explain more, it would help if you elaborated on exactly what the infinite scroll view is.

Upvotes: 1

Related Questions