0

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 %}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    Is that a typo there - `{{ info.inage.url }}`? Should be `image`. – xyres Dec 29 '18 at 07:08
  • Hello xyres, Thank you for mentioning the solution. Yes it was a typo that escape my eyes. Now the link is working but the image is not showing. N:B: I have added height and width in my html file –  Dec 29 '18 at 07:22
  • @MahabubElahiShojib did you include {% load staticfiles %} tag in your template? – Sergey Pugach Dec 29 '18 at 08:14
  • Is the image avilable in right directory e.g. Media? If yes, check

    {{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
  • hello @BidhanMajhi I tried but its not working

    {{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
  • (If you can, please refrain from adding voting advice, or lengthy boilerplate about how much you searched. Readers expect everyone to search, and we surely don't want millions of questions making a point of it `:-)`. If a reader finds a duplicate, then don't worry about it - it is possible that for any given question, you missed one. It happens.) – halfer Dec 29 '18 at 20:39

0 Answers0