2976

Render JSON objects through Django template-tag

Question:

I receive a dictionary of Django objects in a JSON format via AJAX in the template. Is there any possibility to render this dictionary through a Django template-tag? Can I call a Django template-tag from jQuery and transfer the object as a parameter to it?

The current solution is to tediously construct the html in jQuery:

$.ajax({ url: url, type: "POST", success: function(data) { var obj = $.parseJSON(data); $.each(obj, function() { data = this['fields']; post += "<p id='" + this['pk'] + "'>" + data['creator'] + data['created'] + data['body']; post += "depth: " + data['depth']; post += "<a name='" + this['pk'] + "' class='show_answers' href='" + show_url + "'>Show</a>"; post += "<a name='" + this['pk'] + "' href='" + answer_url + "'>Answer</a>

"; post += "<div id='" + this['pk'] + "_div'></div>"; }); $('#' + div_id).html(post); }, crossDomain: false });

Answer1:

Templatetags are server-side, so no, you cannot use django's templating to handle the result of an Ajax request - not directly at least. Possible solutions :

<ul><li>

you can eventually write a view that would take your json data and returns formatted html, but that's going to be a bit on the heavy side.

</li> <li>

if the json comes from your own application, you could return an html fragment instead of json.

</li> <li>

if the json comes from another site, you could also have a view in your app doing the request and returning a formatted html fragment from the json (then you'd post to this view).

</li> <li>

finally if you're app is heavy on ajax/json stuff, you may want to use some js templating framework or something like angular.js

</li> </ul>

Recommend

  • How do I convert a multi dimensional JSON object into javascript array
  • Swift POST Request in same Thread
  • jQuery Datatable: pagination and filter not display correctly
  • How to execute two asynchronous functions sequentially
  • List comprehension with if conditional to get list of files of a specific type
  • Google OAuth: can't get refresh token with authorization code
  • How to load gif image while ajax content is loading and javascript [duplicate]
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • Using a canvas object in a thread to do simple animations - Java
  • Adjust width of select element according to selected option's width
  • MonoTouch: How to download pdf incrementally as indicated in the Apple slides “Building Newsstand Ap
  • Django rest serializer Breaks when data exists
  • Get data from AJAX - How to
  • Bug in WPF DataGrid
  • How to limit post in wp_query
  • Circular dependency while pushing http interceptor
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • How to delete a row from a dynamic generate table using jquery?
  • AngularJs get employee from factory
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Free memory of cv::Mat loaded using FileStorage API
  • using HTMLImports.whenReady not working in chrome
  • How to set the response of a form post action to a iframe source?
  • How do I configure my settings file to work with unit tests?
  • Memory offsets in inline assembly
  • embed rChart in Markdown
  • Change div Background jquery
  • Turn off referential integrity in Derby? is it possible?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?