Send to client character string plus image

Hello

item.first (). picture1 is a .PNG format image, which I need to return ito client from n a view.py function

def funciónGenerica (resquest):
   #algo de código
   rubro=item.objects.filter(provider=proveedores.first()).filter(items=rubro)
   #algo más de código
   return HttpResponse(rubro.first().picture1)

In that case there are no problems in the client, I get the image correctly.

But when:

def funciónGenerica (resquest):
   #algo de código
   rubro=item.objects.filter(provider=proveedores.first()).filter(items=rubro)
   x=rubro.first().titulo #esto es una cadena de caracteres
   imagen=rubro.first().picture1 #esto es una imagen .png
   return HttpResponse(x+imagen)

What I get in the client is a text string and the name (string text) of the image. How is it possible to send the character string followed by the image itself?

From already thank you very much.

Is this being generated as a full page, or is this the result of some type of AJAX call? (How are you handling this response in the browser?)
What does your item model look like?

Basically, what I’m thinking is that you’re trying to send two different mime types in the same response, which is going to require some work on the client side. Assuming that there’s some JavaScript initiating this from the browser, I think your easiest solution is to make these two separate requests.

1 Like

Thanks JenWhitesell.

I really don’t know how to fix the problem. Although it is possible to make 2 requests on the client, I have more than 1 image to send.

The client literally performs a get of all the attributes of the model.

This is the model I must send all the information

class item (models.Model):
    certificate=models.ImageField(default=None)	
    provider = models.ForeignKey(serviceProvider, on_delete=models.CASCADE, null=True)
    radius = models.FloatField(default=None)
    description= models.TextField(blank = True)
    hour_init = models.TimeField()
    hour_end = models.TimeField()
    picture1=models.ImageField(default=None)	
    picture2=models.ImageField(default=None)	
    picture3=models.ImageField(default=None)	

def requestRubros(request, tipo,user, rubro):
    if tipo=="2":
        proveedores=serviceProvider.objects.filter(user=user)
        if proveedores:
            rubro=item.objects.filter(provider=proveedores.first()).filter(items=rubro)
            if rubro:
                x=[]
                img=[]
                x.append(rubro.first().radius)
                x.append(rubro.first().description)
                x.append(rubro.first().hour_init)
                x.append(rubro.first().hour_end)
                x.append(rubro.first().certificate)
                img.append(rubro.first().picture1)
                img.append(rubro.first().picture2)
                img.append(rubro.first().picture3)
                return HttpResponse(x+img)

You haven’t shown what the client is doing to request these, or how it’s handling the response - but I’m almost certain that client changes are going to be necessary to do what you’re looking to do, especially since you’ve now clarified that you’re trying to send multiple images.