Reputation: 71
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
Upvotes: 3
Views: 1262
Reputation: 71
We should strongly use ORM and queries into django models, django can't work with strings from database like with ImageField.
Upvotes: 0
Reputation: 476503
You obtain the url through the .url
attribute [Django-doc]:
<img src="{{ product.product_image.url }}" class="img-responsive watch-right" alt=""/>
Note that on production (so with DEBUG = False
for example), Django does not serve files. In that case, you will need to configure nginx, apache, etc. For more information, see the Static file development view section of the documentation.
Upvotes: 2