Skip to content

Instantly share code, notes, and snippets.

@dminkovsky
dminkovsky / dict_to_view.py
Last active July 10, 2020 01:13
Django: Generate corresponding JSON and rendered template (HTML) views from the same data dictionary.
from django.http import HttpResponse
from django.utils.functional import wraps
from django.template.context import RequestContext
from django.template.loader import get_template
from django.utils import simplejson as json
def dict_to_template_view(**template_args):
"""
Takes a function that returns a dict and decorates it into a template-rendering view
"""