0

Image uploaded by user is not displaying though i have not done wrong in template. Also i have defined MEDIA_URL and MEDIA_ROOT. What might be the reason for not getting image displayed?

Code

    image = models.ImageField(upload_to='products/%Y/%m/%d', blank=True)

project/urls.py(urls.py of main project)

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('shop.urls', namespace='shop')),
] 

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

Settings.py

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')

templates/shop/product/list.html

<a href="{{ product.get_absolute_url }}">
        <img src="{% if product.image %} {{ product.image.url }} {% else %} {% static 'img/no_image.png' %} {% endif %}" >
</a>

I know this question is asked multiple times but my code took after the documentation and still i could not display image. Could anyone help me, please?

pythonBeginner
  • 781
  • 2
  • 12
  • 27

1 Answers1

0

It is working now after fresh restart of the server. So the above code and process of serving media files is correct.

pythonBeginner
  • 781
  • 2
  • 12
  • 27