Tuesday, May 28, 2019

Re: Display image in template

{{ album.album_logo.url }} 

On Wed, 29 May, 2019, 12:08 AM anchal agarwal <agarwalanchal72@gmail.com 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 <anirudhajain.910@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, <agarwalanchal72@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-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CAC3mK7fGps2XR1S7V%2B4WvU-%2Boma-eoUTrtDjEwVJe2QtCTL_Xg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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%3DisWvaOt-NYXGa-Ps5yO7b-bbTfi3%3Dii4wzQmzVCOH%3DO50A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CAKzNicEyaCS_7OFhNU00AsANZ%2Bz%3DpW7_XCL-1pi7Jfo%3DR4bd-Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment