human
Complete this python program:
def handle_response_format(f):
assistant
#Here is the Python code you requested.

    """Handle response format for a view
    """
    def wrap(request, *args, **kwargs):
        'Wrap'
        try:
            if ('response_format' in kwargs):
                response_format = kwargs['response_format']
                if (not response_format):
                    response_format = 'html'
                    kwargs['response_format'] = response_format
                response = f(request, *args, **kwargs)
                if (response_format == 'ajax'):
                    if (response.__class__ == HttpResponseRedirect):
                        location = response['Location']
                        if (not _is_full_redirect(location)):
                            response = HttpResponse(json.dumps({'redirect': location}), content_type=settings.HARDTREE_RESPONSE_FORMATS['ajax'])
                        else:
                            if ('.ajax' in location):
                                location = str(location).replace('.ajax', '')
                            response = HttpResponse(json.dumps({'redirect_out': location}), content_type=settings.HARDTREE_RESPONSE_FORMATS['ajax'])
                    elif hasattr(request, 'redirect'):
                        location = request.redirect
                        response = HttpResponse(json.dumps({'redirect': location}), content_type=settings.HARDTREE_RESPONSE_FORMATS['ajax'])
                    elif (('Content-Disposition' in response) and (response['Content-Type'] not in settings.HARDTREE_RESPONSE_FORMATS.values())):
                        location = request.get_full_path()
                        if ('.ajax' in location):
                            location = str(location).replace('.ajax', '')
                        response = HttpResponse(json.dumps({'redirect_out': location}), content_type=settings.HARDTREE_RESPONSE_FORMATS['ajax'])
                return response
            else:
                return f(request, *args, **kwargs)
        except TemplateNotFound:
            response_format = None
            if ('response_format' in kwargs):
                response_format =...