My problem is I am unable to use images in templates that are uploaded by an admin with admin interface. I have configured my settings and URLs according to documentation.
In browser inspect element it is showing
<img src(unknown)>
Uploaded image by an admin using admin interface is stored in the /media/ which is media root.
Location of the image is
/media/img/imagename.jpg
settings.py
# media
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
urls.py (my project url file)
from django.conf import settings
from django.conf.urls.static import static
## urlpatterns here
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_URL)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_URL)
models.py
class AboutMe(models.Model):
image = models.ImageField(upload_to='img')
## other fields
views.py
def index(request):
aboutme = models.AboutMe.objects.all()
context = {'information': aboutme,}
return render(request, 'portfolio/index.html', context)
template/index.html
{% for info in information %}
<img src="{{ info.inage.url }}"/>
<div class="name-profile t-center">
<h5 class="uppercase">{{info.name}}</h5>
</div>
{% endfor %}
{{info.image}}
is it showing the correct path? And also remove this line, `urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_URL)` – Bidhan Majhi Dec 29 '18 at 09:51{{info.image}}
showing img/image.jpg actual image location in /media/img/jpg so i guess the url is working – Dec 29 '18 at 12:50