Skip to content Skip to sidebar Skip to footer

Want To Display An Image

I am having a slight problem. I want a django app that can upload and display an image. Currently, it can upload the image but I cannnot display that image. So for example, {{comm

Solution 1:

{% if comment.photo %} <img src="{{ comment.photo.url }}" alt="Photo" /> {% endif %}

See Geoffrey's comment for how to upload the image correctly.

Solution 2:

The upload parameter of ImageField must be a local path, so replace:

photo = models.ImageField(upload_to='C:/Users/AQUIL/Desktop/myproject/media/images', blank=True, null=True)

by:

photo = models.ImageField(upload_to='images', blank=True, null=True)

Then set the MEDIA_ROOT in settings.py as:

MEDIA_ROOT = 'C:/Users/AQUIL/Desktop/myproject/media/'

Finally your image 'myImage.png' will be accessible at:

C:/Users/AQUIL/Desktop/myproject/media/images/myImage.png

And this tag should load the image:

<imgsrc="/media/images/myImage.png"alt=""/>

depends of your MEDIA_URL in settings.py which should be:

MEDIA_URL = '/media/'

Solution 3:

Rather then {{comment.photo}}

use {{comment.photo.url}}

see sample in the docs

Post a Comment for "Want To Display An Image"