Answer the question
In order to leave comments, you need to log in
Why is the image not displayed on .values()?
views.py
def article(request, c_name):
context = {
'page_img': Article.objects.filter(category=c_name).values('image').first()
# ...
}
return render(request, 'app/page.html', context)
...
<img src="{{ page_img.image.url }}">
...
Answer the question
In order to leave comments, you need to log in
values('image') will return you what it should - the value of the field in the database. And this is the relative URL of the image.
{'page_url': Article.objects.filter(category=c_name).values('image').first()['image']}
...
<img src="{{ page_url }}">
...
{'page_url': Article.objects.filter(category=c_name).values_list('image', flat=True).first()}
...
<img src="{{ page_url }}">
...
str(Photo.objects.values('image').query)
Out[7]: 'SELECT "catalog_photo"."image" FROM "catalog_photo" ORDER BY "catalog_photo"."position" ASC'
str(Photo.objects.only('image').query)
Out[8]: 'SELECT "catalog_photo"."id", "catalog_photo"."image" FROM "catalog_photo" ORDER BY "catalog_photo"."position" ASC'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question