How can I pass an image to a template in Django?

Everyone_Else

Suppose that the corresponding function in views.py looks like

from PIL import Image
def get_img(request, img_source)
    base_image = Image.open(os.getcwd() + '/deskprod/media/img/'+ img_source + ".png")
    #Some editing of base_image done with PIL that prevents image from being directly loaded in html
    return render_to_response('get_img.html', {
        'base_image': base_image},
        context_instance=RequestContext(request))

How can I then display base_image in the get_img.html template?

Kamil Zajac

You should process the image, save it on local disk and then send a path to it or more like the media url which will correspond to that image as context to html template. You need to configure your django server to serve static and media files to do that and configure serving those files in production environment as well. Read more here https://docs.djangoproject.com/en/1.9/howto/static-files/

However it should be possible to create dynamic image and serve with django in the fly with PIL if you can't or really do not want to save it locally. It will be sth like at the and of you code you should add.

response = HttpResponse(mimetype="image/png")
base_image.save(response, "PNG")
return response

Check also more info http://effbot.org/zone/django-pil.htm, it may work, although I didn't test it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I pass a Django template context variable to JS function

From Dev

Django template blocks: how can i reuse them and how do i pass HTML into them?

From Dev

How can I pass a template to a directive in an attribute?

From Dev

How can i pass image array to JSONObject?

From Dev

How can I validate the syntax of a django template?

From Dev

How can I get this Django template to render?

From Dev

How can I efficiently render an image in Django?

From Dev

How can I pass an array as Input() from the component template?

From Dev

How can I pass floating point numbers as template parameters?

From Dev

How I can pass lambda expression to c++ template as parameter

From Dev

How can I pass a error 500 message to my error template?

From Dev

Meteor - How can I pass data between helpers and events for a template?

From Dev

How can I pass multiple variables into a template in Webpy?

From Dev

How can I pass an array to an ejs template in express?

From Dev

how can I pass dynamically data to a knockout template binding

From Dev

How can I pass floating point numbers as template parameters?

From Dev

How can I pass multiple variables into a template in Webpy?

From Dev

Jade: how can I pass a random variable into included template?

From Dev

How to pass a variable to template in Django

From Dev

How can I create a template image to be colored at run time

From Dev

How can I embed a PNG image in a stack (mustache) template?

From Dev

How can I display an ImageField image in my template?

From Dev

Django: How to render image with a template

From Dev

How can I pass extra variables with image over an AJAX call

From Dev

How can I pass parameter when upload image?

From Java

How can I use Django DeleteView in my template

From Dev

(How) can I tell if my form field is hidden in Django template

From Java

How can I count my followers in django template?

From Dev

How can I modify the basic Django template for SplitDateTimeField?

Related Related

  1. 1

    How can I pass a Django template context variable to JS function

  2. 2

    Django template blocks: how can i reuse them and how do i pass HTML into them?

  3. 3

    How can I pass a template to a directive in an attribute?

  4. 4

    How can i pass image array to JSONObject?

  5. 5

    How can I validate the syntax of a django template?

  6. 6

    How can I get this Django template to render?

  7. 7

    How can I efficiently render an image in Django?

  8. 8

    How can I pass an array as Input() from the component template?

  9. 9

    How can I pass floating point numbers as template parameters?

  10. 10

    How I can pass lambda expression to c++ template as parameter

  11. 11

    How can I pass a error 500 message to my error template?

  12. 12

    Meteor - How can I pass data between helpers and events for a template?

  13. 13

    How can I pass multiple variables into a template in Webpy?

  14. 14

    How can I pass an array to an ejs template in express?

  15. 15

    how can I pass dynamically data to a knockout template binding

  16. 16

    How can I pass floating point numbers as template parameters?

  17. 17

    How can I pass multiple variables into a template in Webpy?

  18. 18

    Jade: how can I pass a random variable into included template?

  19. 19

    How to pass a variable to template in Django

  20. 20

    How can I create a template image to be colored at run time

  21. 21

    How can I embed a PNG image in a stack (mustache) template?

  22. 22

    How can I display an ImageField image in my template?

  23. 23

    Django: How to render image with a template

  24. 24

    How can I pass extra variables with image over an AJAX call

  25. 25

    How can I pass parameter when upload image?

  26. 26

    How can I use Django DeleteView in my template

  27. 27

    (How) can I tell if my form field is hidden in Django template

  28. 28

    How can I count my followers in django template?

  29. 29

    How can I modify the basic Django template for SplitDateTimeField?

HotTag

Archive