image in browser

37 views
Skip to first unread message

Satvir Toor

unread,
Jun 14, 2012, 2:15:19 AM6/14/12
to django...@googlegroups.com
hello,

i wish to display the image into the browser through a template . I
used the following code to retrieve the image from Disk and send that
data to Html file

def my_image(request):
image_data = open("/home/toor/Desktop/certificate/logo.png", "rb").read()
return render_to_response('myapp/bio.html', {'img':image_data},
context_instance=RequestContext(request))


Now, The image is not availble into the html file that is displaying
in browser, Tell me where I m wrong.


--
Satvir Kaur

Daniel Roseman

unread,
Jun 14, 2012, 4:40:46 AM6/14/12
to django...@googlegroups.com
This is a strange thing to want to do. What are you doing with `img` in the template?
--
DR.

Satvir Toor

unread,
Jun 14, 2012, 4:51:00 AM6/14/12
to django...@googlegroups.com
> This is a strange thing to want to do. What are you doing with `img` in the
> template?
I am using image_data variable as a image source in the template(html file).

<img src="{{img}}" width="100" height="100" alt="image is not
available" align="right"/>
What should i ??? Image is not displaying.



--
Satvir Kaur
satveerkaur.blogspot.in

Daniel Roseman

unread,
Jun 14, 2012, 5:02:39 AM6/14/12
to django...@googlegroups.com
On Thursday, 14 June 2012 09:51:00 UTC+1, Satvir Kaur wrote:
> This is a strange thing to want to do. What are you doing with `img` in the
> template?
I am using image_data variable as a image source in the template(html file).

<img src="{{img}}" width="100" height="100" alt="image is not
available" align="right"/>
 What should i ??? Image is not displaying.

Normally in an HTML file you put the image path in the src attribute, not the binary data (you can use a data URI with base64 encoding, but you almost certainly don't want to do that). 
--
DR.

kooliah

unread,
Jun 14, 2012, 5:05:43 AM6/14/12
to django...@googlegroups.com

Satvir Toor

unread,
Jun 14, 2012, 5:16:18 AM6/14/12
to django...@googlegroups.com
Yes Sir,
when i put whole path of image in src attribute its not working.


--
Satvir Kaur
satveerkaur.blogspot.in

kenneth gonsalves

unread,
Jun 14, 2012, 6:03:43 AM6/14/12
to django...@googlegroups.com
On Thu, 2012-06-14 at 14:21 +0530, Satvir Toor wrote:
> > This is a strange thing to want to do. What are you doing with `img`
> in the
> > template?
> I am using image_data variable as a image source in the template(html
> file).
>
> <img src="{{img}}" width="100" height="100" alt="image is not
> available" align="right"/>
> What should i ??? Image is not displaying.
>
>

from the questions you have been asking on the list it seems to me that
you are unfamiliar with web programming. I suggest that you take some
time out to learn the basics of web programming and also to understand
how django works. I suggest you do the tutorial in the official
documentation. Also you should learn at least the basics of javascript.
The problem you have been bringing up in the last week is not best
solved by generating an image on the server side and passing it to the
template. The problem is solved by generating the data server side,
passing this data to the template and using a js library to generate and
render the image on the page.
--
regards
Kenneth Gonsalves

Ivan Ivanov

unread,
Jun 14, 2012, 2:34:03 AM6/14/12
to django...@googlegroups.com
На Thu, 14 Jun 2012 11:45:19 +0530
Satvir Toor <toor.s...@gmail.com> написа:

> hello,
>
> i wish to display the image into the browser through a template . I
> used the following code to retrieve the image from Disk and send that
> data to Html file
>
> def my_image(request):
> image_data = open("/home/toor/Desktop/certificate/logo.png",
> "rb").read() return render_to_response('myapp/bio.html',
> {'img':image_data}, context_instance=RequestContext(request))
>

Why don't you use static files?

> Now, The image is not availble into the html file that is displaying
> in browser, Tell me where I m wrong.

You better show us the template too.

Christian Jurk

unread,
Jun 14, 2012, 3:36:21 AM6/14/12
to django...@googlegroups.com
Why would you do that this way? The usual way would be to put your
images into the static files and serve it from there. However, you
should be able to do it that way using base64 encoding of the binary data:

from base64 import b64encode

def my_image(request):
image_data = open("/home/toor/Desktop/certificate/logo.png",
"rb").read()
return render_to_response('myapp/bio.html', {'img':
b64encode(image_data)},
context_instance=RequestContext(request))

After that, you can output your image inside the template by using

<img src="data:image/png;base64,{{ img }}" alt="Embedded image" />



On 06/14/2012 08:15 AM, Satvir Toor wrote:

Ivan Ivanov

unread,
Jun 14, 2012, 4:58:26 AM6/14/12
to django...@googlegroups.com
На Thu, 14 Jun 2012 14:21:00 +0530
Satvir Toor <toor.s...@gmail.com> написа:

> > This is a strange thing to want to do. What are you doing with
> > `img` in the template?
> I am using image_data variable as a image source in the template(html
> file).
>
> <img src="{{img}}" width="100" height="100" alt="image is not
> available" align="right"/>
> What should i ??? Image is not displaying.

Use static files for that:
https://docs.djangoproject.com/en/dev/howto/static-files/

Satvir Toor

unread,
Jun 14, 2012, 7:13:40 AM6/14/12
to django...@googlegroups.com
> You better show us the template too.
Problem solved.
Image is displaying just putting the image into the localhost
directories(/var/www/logo.png)
and giving the src attribute as
<img src="http://localhost/logo.png" `width="200" height="200"
alt="image is not available" align="right"/>


--
Satvir Kaur
satveerkaur.blogspot.in

Satvir Toor

unread,
Jun 14, 2012, 7:16:30 AM6/14/12
to django...@googlegroups.com
On Thu, Jun 14, 2012 at 3:33 PM, kenneth gonsalves
<law...@thenilgiris.com> wrote:
> from the questions you have been asking on the list it seems to me that
> you are unfamiliar with web programming. I suggest that you take some
> time out to learn the basics of web programming and also to understand
> how django works. I suggest you do the tutorial in the official
> documentation. Also you should learn at least the basics of javascript.
> The problem you have been bringing up in the last week is not best
> solved by generating an image on the server side and passing it to the
> template. The problem is solved by generating the data server side,
> passing this data to the template and using a js library to generate and
> render the image on the page.
>
thnx Sir. I will do that.


--
Satvir Kaur
satveerkaur.blogspot.in
Reply all
Reply to author
Forward
0 new messages