Asaduzzaman Sohel Asaduzzaman Sohel - 3 years ago 222
Python Question

Not properly render image from Django model

I am making a simple app(Django version 1.11+python3.5+). I want to show images on my homepage(web app) from Django models. I can upload successfully from Django admin panel. But image cant renders properly. The image does not show in the homepage. This is my HTML file.

{% extends "shop/base.html" %}
{% block content_area %}
{% for name in products %}
<div class="col-lg-3 col-md-3">
<div class="main_content_sidebar">
<div class="content_title">
<div class="image_space">
<img src="{{name.product_image.url}}" class="img-thumbnail" alt="">
<div class="content_p">
<p>Retail Price:{{name.product_retail_price}}</p>
<p>Quantity: {{name.product_quantity}}</p>
<p>Date: {{name.product_sell_date}}</p>
{% endfor %}
{% endblock %}

this is mysite(project)>>

from django.conf.urls import url, include
from django.contrib import admin
from django.conf.urls.static import static

from django.conf import settings
urlpatterns = [
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,

This is my file.

class ProductsName(models.Model):
product_name=models.CharField('name', max_length=50)
product_retail_price=models.IntegerField('retail price TAKA')
product_quantity = models.IntegerField('quantity')
product_sell_date=models.DateTimeField('buy date', auto_now_add=True)
product_image = models.ImageField(upload_to='upload/', blank=True, null=True)

def __str__(self):
return self.product_name file

Django settings for mysite project.

Generated by 'django-admin startproject' using Django 1.11.5.

For more information on this file, see

For the full list of settings and their values, see

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See

# SECURITY WARNING: keep the secret key used in production secret!

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True


# Application definition



ROOT_URLCONF = 'mysite.urls'

'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'context_processors': [

WSGI_APPLICATION = 'mysite.wsgi.application'

# Database

'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

# Password validation

'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',

# Internationalization


TIME_ZONE = 'Asia/Dhaka'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)

STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'shop/images/')
MEDIA_URL = '/media/'

When I visit my homepage, I can't see any image. But I see image link If I open my browser dev tools.
chrome dev tool

if I click that image link from chrome dev tool. I got andjango doesn/t found that image url error.

Also my folder structure is:


Answer Source

The issue is that the your MEDIA_ROOT is not being served and Django cannot append the correct url. In your project add the following at the end:

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

And in the add the correct paths for:

MEDIA_ROOT = os.path.join(BASE_DIR,'path_to_/media_folder')
MEDIA_URL = '/media/'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download