Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top