Django media image not showing up in template but can acces it via …/media/thumbnail/image.png

Question:

Altough context does render in html, i can use other variables but cant accsess the image. My code is:

html:

    {% for a in Manga %}
        <div class="manga_container">
            <div class="manga_image">
                <p>{{a.manga_image}}</p>
                <img src="{{ x.manga_image.url }}" alt="">
            </div>
        </div>
    {%endfor%}

model:

class Manga(models.Model):
    manga_name = models.CharField(max_length= 255, default="null")
    manga_image = models.ImageField(upload_to="thumbnail", default="thumbnail/noimage.png")

    def __str__(self):
        return f"{self.id}, {self.manga_name}"

and lastly view:

def Index(request):
    manga = Manga.objects.all().values()
    chapter = Chapter.objects.all().values()
    fansub = Fansub.objects.all().values()

    context= {
        "Manga": manga,
        "Chapter": chapter,
        "Fansub": fansub,
    }

    template = loader.get_template("Index.html")

    return HttpResponse(template.render(context, request))

ı have used function based views since ı have to deal with multiple models at once.

my media and url is as following:

import os

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urlpatterns = [
    ...
    ...
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

I have put that p tag for me to see its url and as you can see it was in the right place, when i go to 127.0.0.1:8000/media/thumbnail/noimage.png i can see it and it does come up to my screen. i have tried most of the ways i could have think of. I want to see the image on my html and i didnt touch anything so it is basicly default image as i have configured it to.

Asked By: PlatinMavi

||

Answers:

Instead of:

Manga.objects.all().values()

Use:

Manga.objects.all()
Answered By: Manoj Kamble