Display image in template

14 views
Skip to first unread message

anchal agarwal

unread,
May 28, 2019, 2:01:20 PM5/28/19
to django...@googlegroups.com
I want to display image dynamically from the database in a django template. I have used ImageField for this purpose. The code shows no error but it is only displaying an icon of image.
This is my template file,
here album is the context and album_logo is the variable in which i have stored my image. please tell me how can i fix this issue
<img src="{{ album.album_logo}}">

<h1>{{ album.album_title}}</h1>
<h3>{{ album.artist }}</h3>

<ul>
{% for song in album.song_set.all %}
<li>{{song.song_title}}-{{song.file_type}}</li>
{% endfor %}
</ul>

Anirudh Jain

unread,
May 28, 2019, 2:12:32 PM5/28/19
to django...@googlegroups.com
You will have to setup MEDIA path f8rst. All your uploded files will be stored in that folder. STATIC path is used only for, well, static content like css and js.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMT%3DisWxuO9d1tTMacyYauDAmQS%2B2e_B9HawfZP7eY8smCS%3D2Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

anchal agarwal

unread,
May 28, 2019, 2:38:49 PM5/28/19
to django...@googlegroups.com
Thanks for the answer but  I have already set the MEDIA path 
here is my urls.py
from . import settings
from django.contrib.staticfiles.urls import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

models.py
from django.db import models

class Album(models.Model):
artist = models.CharField(max_length=250)
album_title = models.CharField(max_length=500)
genre=models.CharField(max_length=100)
album_logo = models.ImageField(upload_to="gallery")

def __str__(self):
return self.album_title


 settings.py
import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

MEDIA_ROOT=os.path.join(BASE_DIR,'media')
MEDIA_URL= '/media/'

Nitin Kumar

unread,
May 28, 2019, 2:58:48 PM5/28/19
to django...@googlegroups.com

Joe Reitman

unread,
May 28, 2019, 3:08:56 PM5/28/19
to Django users
Have you looked in inspector on your browser to see if the URL is correctly rendered in IMG SRC?
Have you installed Pillow? ImageField requires python Pillow.

On Tuesday, May 28, 2019 at 1:38:49 PM UTC-5, Anchal Agarwal wrote:
Thanks for the answer but  I have already set the MEDIA path 
here is my urls.py
from . import settings
from django.contrib.staticfiles.urls import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

models.py
from django.db import models

class Album(models.Model):
artist = models.CharField(max_length=250)
album_title = models.CharField(max_length=500)
genre=models.CharField(max_length=100)
album_logo = models.ImageField(upload_to="gallery")

def __str__(self):
return self.album_title


 settings.py
import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

MEDIA_ROOT=os.path.join(BASE_DIR,'media')
MEDIA_URL= '/media/'

On Tue, May 28, 2019 at 11:42 PM Anirudh Jain <anirudh...@gmail.com> wrote:
You will have to setup MEDIA path f8rst. All your uploded files will be stored in that folder. STATIC path is used only for, well, static content like css and js.

On Tue, 28 May 2019, 23:31 anchal agarwal, <agarwal...@gmail.com> wrote:
I want to display image dynamically from the database in a django template. I have used ImageField for this purpose. The code shows no error but it is only displaying an icon of image.
This is my template file,
here album is the context and album_logo is the variable in which i have stored my image. please tell me how can i fix this issue
<img src="{{ album.album_logo}}">

<h1>{{ album.album_title}}</h1>
<h3>{{ album.artist }}</h3>

<ul>
{% for song in album.song_set.all %}
<li>{{song.song_title}}-{{song.file_type}}</li>
{% endfor %}
</ul>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

anchal agarwal

unread,
May 28, 2019, 4:06:09 PM5/28/19
to django...@googlegroups.com
Thanks Nitin. It worked !!
Reply all
Reply to author
Forward
0 new messages