'render media file to html template from views.py django
I am trying to show image from views.py to jinga2 html template.
Here is code
models.py
class SkinModel(models.Model):
pic=models.ImageField(upload_to='images',blank=True)
forms.py
class SkinForm(forms.ModelForm):
class Meta:
model=SkinModel
fields = "__all__"
views.py
def home(request):
if request.method=='POST':
form = SkinForm(request.POST,request.FILES)
if form.is_valid():
img=form.cleaned_data['pic']
if img:
fs = FileSystemStorage()
filename = fs.save(img.name, img)
path = fs.url(filename)
print(path)
return render(request,'home.html',{'form':form,'path':path})
home.html
{% load static %}
<img src="{{ path }}" ,width="500" height="400">
the print statement shows the path like /media/bcc_38Mf6gh.jpg but i dont know how to render it on html template
Solution 1:[1]
Install Pillow
pip install Pillow
And then add .url to path:
{% load static %}
<img src="{{ path.url }}" ,width="500" height="400">
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jero Guzman |
