0

I am using dajax for ajax in my django app. After getting some data from database I create list of <li> elements in my python ajax.py and assign it with dajax to inner html of some container. Like this:

@dajaxice_register
def get_transactions(request):
    dajax = Dajax()
    transactions = get_transactions()
    dajax.assign('#transactions', 'innerHTML', ''.join(transactions))
    return dajax.json()

What is considered best practice? Returning html from server or returning json and then creating html in script?

Vladimir Nani
  • 2,774
  • 6
  • 31
  • 52
  • I don't know much bout python, but I prefer letting the server create the html and I just dump it into some container element. – Nilesh Aug 08 '13 at 16:42
  • I always have the server return JSON objects and then have the client do whatever they want with that data, IE make a UI or HTML – abc123 Aug 08 '13 at 16:49
  • I always prefer to return the json. But [this](http://stackoverflow.com/questions/1145526/best-practice-loading-rendered-html-or-json) might also help you. – sawan gupta Aug 08 '13 at 19:01

2 Answers2

1

I would return JSON from the server and bind that to the DOM using JavaScript. That way you're keeping concerns separate, and also returning the minimum amount of data from the server.

Brandon Taylor
  • 33,823
  • 15
  • 104
  • 144
1

Always returning JSON helps to create a services oriented architecture with a good separation between your view and your controller (and model).

With this approach, you can have a pure-HTML UI and a REST API to retrieve datas from server.

I think it's a good practice, but it's probably better for Web Application and not for generic websites.

Loic
  • 451
  • 3
  • 6