2

I have a webpage which will show the logo of an music album. For that I've added a database field named album_ logo and add the path name of the images folder "C:\Python Projects\Test_App\website\music\static\music\Image" in database. But in the HTML page album_logo is not rendering.

Here is the attached code snippet. <body> {% load static %} <img src = "{{album.album_logo}}"> <h1>{{album.album_title}}</h1> <h3>{{album.artist}} - {{album.genre}}</h3> </body> 

Create your models here.

class Album(models.Model): album_title = models.CharField(max_length=250) artist = models.CharField(max_length=250) genre = models.CharField(max_length=100) album_logo = models.CharField(max_length=500) def __str__(self): return self.album_title + '-' + self.artist 

Example: For the first album I've added the image path in database as - "C:\Python Projects\Test_App\website\music\static\music\Image\The_Division_Bell.jpg"

5
  • 1
    please post your models.py too :) Commented Aug 17, 2019 at 6:12
  • does the image have successfully uploaded into your database Commented Aug 17, 2019 at 6:33
  • 1
    @SubhajitPodder you need to add that model into your question not in the comment.Please edit your question with your model Commented Aug 17, 2019 at 6:38
  • album_logo should be image field not a charfield since it is a image Commented Aug 17, 2019 at 6:46
  • @user11418935 But I've added the path of the image inside double quotation not directly adding the image in DB. Commented Aug 17, 2019 at 6:46

1 Answer 1

2

album_logo should be ImageField since it is a image. Also you need to do pip install pillow to upload the images.

In your models

....................... album_logo = models.ImageField(upload_to='alubm_logo') 

and in your project's urls.py you need to provide the static url like this (Note:this is only for development)

project/urls.py

urlpatterns=[....... ................ ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

settings.py

MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media') 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for assistance. I've a few more things to ask, I've music app inside the website. Where shall I add the MEDIA_URL and MEDIA_ROOT? What will be the values of these? I.e [MEDIA_URL = static\music\Image]
in your settings.py it should be by default.If not you can add it.see my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.