[Answer]-Render a template in block without loading the whole page

1👍

You have a couple of choices: Ajax or iFrames

If you go the Ajax route, you can render a template server-side and return the HTML in the Ajax response:

import json
from django.http import HttpResponse
from django.template.loader import render_to_string

def render_template(request):
    result = {'html': render_to_string('your-template.html', {})}
    return HttpResponse(json.dumps(result, ensure_ascii=False),
        content_type='application/json')

Then you can use jQuery or whatever you want to update the HTML in the main page wherever you need.

Alternatively, you can use JavaScript to set the src of an iFrame on click to render a view without updating the entire page. It just depends on what’s best for you in terms of user experience, maintenance, etc.

Leave a comment