Wednesday, October 27, 2010

Re: How to display image in django template

Another way for static images:
Its good to have all html related stuffs (html files, images, ..etc) in one directory (ex: html directory)

In settings.py there is a field called TEMPLATE_DIR, add the absolute-path to the html directory to this filed
ex: TEMPLATE_DIR = 'path-to-html-directory',

in template, access the image as <img src="path to html dir">    


On Mon, Oct 25, 2010 at 4:12 PM, Kenneth Gonsalves <lawgon@au-kbc.org> wrote:
On Mon, 2010-10-25 at 03:34 -0700, d wrote:
> I have been searching for solutions whole day.
> How would I display image file in django template. I have already set
> MEDIA_ROOT and MEDIA_URL.

assume a model:

Mymodel(models.Model):
photo = models.ImageField("somename",upload_to="images/")

get an instance:

myob = Mymodel.objects.get(pk=1)

in your template:

<img src="{% myob.photo.url %}">
--
regards
Kenneth Gonsalves
Senior Associate
NRC-FOSS at AU-KBC

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--
Girish M S

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment