Skip to content

Instantly share code, notes, and snippets.

@FZambia
Created December 10, 2012 13:54
Show Gist options
  • Save FZambia/4250684 to your computer and use it in GitHub Desktop.
Save FZambia/4250684 to your computer and use it in GitHub Desktop.
Render tastypie data into template (initially for debugging with django-debug-toolbar)
def tastypie_template(template_name="base.html", var_name='data', serialize=True):
"""
Decorator.
Wrap Tastypie Resource class with it to render into template.
Usage:
@tastypie_template()
class UserResource(ModelResource):
...
@param template_name: template to render
@param var_name: variable name in template
@param serialize: serialize data or not
"""
def outer(original_class):
def create_response(self, request, data, **kwargs):
if serialize:
desired_format = self.determine_format(request)
data = self.serialize(request, data, desired_format)
from django.template.response import TemplateResponse
return TemplateResponse(request, template_name, {var_name: data})
original_class.create_response = create_response
return original_class
return outer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment