Issue
I have a model that has a field of Image
profile_picture = models.ImageField(upload_to='clothes_images', null=True, blank=True)
.
I have a function that returns the image URL
def image_url(self):
if self.profile_picture:
return self.profile_picture.url
return None
but when I display in my template using this code <img src="{{cloth.image_url}}">
I get this error Not Found: /media/clothes_images/1_fSqDIIb.png
my image name is 1.png but it changes when I store that and not find that.
I want to display each image that stores in my local system.
this is my setting.py file for media MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
also I added this code to my urls.py file
if settings.DEBUG: urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
Solution
If you store the image as base64
encoded into your database you can show it into html:
# pip install pybase64
import base64
with open("image.jpg", "rb") as imagefile:
convert = base64.b64encode(imagefile.read())
print(convert.decode('utf-8'))
# in html page the tag for showing the image looks like
# <img src="data:image/png;base64,iVBORw0KG..." />
Answered By - Hermann12
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.