from django.http import HttpResponse from django.template import RequestContext, loader from django.template.loader_tags import BlockNode from simplejson import dumps def ajax_render_to(template_name): def deco_wrap(func): def wrap(request, *args, **kwargs): res = func(request, *args, **kwargs) if type(res) is not dict: return res template = loader.get_template(template_name) cont = RequestContext(request, res) if not 'ajax_request' in request.GET: return HttpResponse(template.render(cont)) blocks = [] for node in template.nodelist: blocks = blocks + node.get_nodes_by_type(BlockNode) result = {} for block in blocks: result["block__%s"%block.name] = block.render(cont) return HttpResponse(dumps(result)) return wrap return deco_wrap