[Answered ]-Put a Rendered Django Template in Json along with some other items

2👍

Use render_to_string() shortcut and JsonResponse:

from django.http import JsonResponse
from django.template.loader import render_to_string


def view_name(request, params):
    objects = Object.objects.all()
    somevar = "something"
    request_id = 123456

    # Context to be passed on to template
    context = {'objects':objects}

    rendered_html = render_to_string('appname/template.html', context)

    return JsonResponse({
        "somevar": somevar,
        "html": rendered_html,
        "request_id": request_id
    })
👤alecxe

Leave a comment