1

I have a method in views.py that creates an image and also creates a text string to return to a template for display. The string is dependent on the dynamically generated image, so I need the image to be created, then create the string, then return both to a view. I am not using a backend currently due to not desiring to keep photos, and strings around taking up space (user does not need it).

Trying different things below:

This code shows just the image correctly(without the string showing up since not returned):

response = HttpResponse(content_type='image/png')
image.save(response, "PNG")
return response

This code just displays text:

    ##return text
    response=HttpResponse()
    response.write(the_string)
    return response

This code doesn't quite work right as it just displays text (the PIL photo object text string instead of the image, the string is displayed correctly)

objects_to_render = {"photo":image,"string":the_string}
return render(request, "pic_text_display.html",objects_to_render, content_type='image/png')

In "pic_text_display.html":

{{photo}}
{{string}}

Here is the output from above:

<PIL.Image.Image image mode=RGBA size=512x512 at 0x7FCEA377FED0>
the string

How can I return an image and have the string available for display in the template at the same time? I am sure there is a better way to do this than I have been...

Note that I am not using a backend model as I am not storing the attribute, just want to create the string and image, and return both for display. I seem to be having issues as I have different content types, or don't know the right syntax.

Thanks for the help!!

JJSanDiego
  • 721
  • 5
  • 11
  • This could probably give you some insight. http://stackoverflow.com/questions/10399077/django-httpresponse-response-with-file-and-content – JClarke Jul 27 '16 at 21:48
  • Hmm.. took a look, but still a little confused. Is it possible to pass in the dictionary back to the template, both the image, and the string? The image is dynamically generated along with the string... with the return render(request, "pic_text_display.html",objects_to_render, content_type='image/png').. they are both available, but the picture does not render. I need the picture render, and then display the text. thoughts? – JJSanDiego Jul 27 '16 at 22:11
  • If it's a small image you can also convert the image object to a base64 string (http://stackoverflow.com/questions/31826335/how-to-convert-pil-image-image-object-to-base64-string). Then use data-uri (https://css-tricks.com/data-uris/) format to include it in your HTML. – Philip Tzou Jul 27 '16 at 22:52
  • Philip - Thanks for the response. That is an interesting solution that I hadn't thought of. Still, I can't guarantee a small image since this is user uploaded, and the string is being output from the image. There is a dependency between the two. This appears to be mostly a problem with the dynamic image still needs a url to serve properly, as the dictionary context returns the location of the image object. there is a solution here for rendering a dynamic image to template, but I am really as a loss of how to implement, because in one post request I need to gen the image and return a string. – JJSanDiego Jul 27 '16 at 23:03
  • this is the link with rendering dynamic to template. not a full answer here as I am not sure how to have one method just return the image, but then another method needs the image for the string, and return all in one response. http://stackoverflow.com/questions/16358091/django-serve-dynamic-reportlab-png-to-template – JJSanDiego Jul 27 '16 at 23:03

0 Answers0