[Answered ]-Return JSON response using Django template system

2👍

simple approach is to render first the data from your template to string and response it as json like the sample code below.

from django.template.loader import render_to_string
from django.http import HttpResponse
def jsonview(request):
      context = {}
      context['data'] = render_to_string("json.html", {'date': datetime.now()})
      return HttpResponse(json.dumps(context), content_type="application/json")

0👍

You should use Django templatetags. You can create your own custom template tag or use built-in ones

In this case you could use templatetag now in your json.html file:

{% now "SHORT_DATETIME_FORMAT" %}
👤Sasa

Leave a comment