def render_to_response(template_name, context=None, context_instance=None, response_format='html'):
    if context is None:
        context = {}
    if not response_format:
        response_format = 'html'

    if response_format not in settings.HARDTREE_RESPONSE_FORMATS:
        response_format = 'html'

    content_type = settings.HARDTREE_RESPONSE_FORMATS[response_format]

    if 'pdf' in response_format:
        while True:
            hasher = hashlib.md5()
            hasher.update(str(random.random()))
            filepath = u"pdfs/" + hasher.hexdigest()
            output = settings.MEDIA_ROOT + filepath
            if not os.path.exists(output + ".pdf"):
                break

        while True:
            hasher = hashlib.md5()
            hasher.update(str(random.random()))
            filepath = hasher.hexdigest() + ".html"
            source = getattr(settings, 'WKCWD', './') + filepath
            if not os.path.exists(source):
                break

        page_size = "A4"
        orientation = "portrait"

        rendered_string = render_to_string(
            template_name, context, context_instance, response_format)

        f = codecs.open(source, encoding='utf-8', mode='w')
        pdf_string = unicode(rendered_string)

        if context_instance and context_instance['request']:
            pdf_string = pdf_string.replace(
                "a href=\"/", "a href=\"http://" + RequestSite(context_instance['request']).domain + "/")

        pdf_string.replace("href=\"/", "href=\"")

        pattern = """Content-Type: text/html|<td>\n\W*<div class="content-list-tick">\n\W.*\n.*</div></td>|<th scope="col">Select</th>"""
        pdf_string = re.sub(pattern, "", pdf_string).replace(
            '/static/', 'static/')

        f.write(pdf_string)
        f.close()

        wkpath = getattr(settings, 'WKPATH', './bin/wkhtmltopdf-i386')
        x = subprocess.Popen("%s --print-media-type --orientation %s --page-size %s %s %s" %
                             (wkpath,
                              orientation,
                              page_size,
                              source,
                              output),
                             shell=True,
                             cwd=getattr(settings, 'WKCWD', './'))
        x.wait()

        f = open(output)
        response = HttpResponse(f.read(), content_type='application/pdf')
        f.close()

        os.remove(output)
        os.remove(source)

        # response['Content-Disposition'] = 'attachment; filename=%s'%(pdf_name)

        return response

    if 'ajax' in response_format:
        rendered_string = render_to_ajax(
            template_name, context, context_instance)

    else:

        if response_format == 'html' and context_instance and context_instance['request'].path[:3] == '/m/':
            context['response_format'] = response_format = 'mobile'

        if getattr(settings, 'HARDTREE_FORCE_AJAX_RENDERING', False):
            context = preprocess_context_ajax(context)

        rendered_string = render_to_string(
            template_name, context, context_instance, response_format)

    response = HttpResponse(rendered_string, content_type=content_type)

    return response
Extended render_to_response to support different formats
dataset/ETHPy150Open treeio/treeio/treeio/core/rendering.py/render_to_response