Django doesn't want to show media files

Viewed 909

I'm trying to output an image which i save like this:

product_image = models.ImageField(blank = True, upload_to='images')

My seetings.py looks like:

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

Also here is urls.py:

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^$', include('store.urls')),
    url(r'^', include('store.urls')),

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Query is like simple implemetation of ORM, but i tried it on ORM also

def index(request):
products = DAO.get_all_products()
return render(request, 'store/index.html', locals())

HTML:

<img src="{{product.product_image}}" class="img-responsive watch-right" alt=""/>

And it doesn't work, i don't see any image, and {{product.product_image}} shows me images/pic13.jpg in browser. But i can see this image in /admin Error:

Not Found: /images/pic13.jpg

How to fix this issue? This page is situated is App called store, urls.py and settings.py are in main app

2 Answers

We should strongly use ORM and queries into django models, django can't work with strings from database like with ImageField.

Related