In a view, how can I return the MEDIA_URL for Imagefield queryset?
source link: https://www.codesd.com/item/in-a-view-how-can-i-return-the-media-url-for-imagefield-queryset.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
In a view, how can I return the MEDIA_URL for Imagefield queryset?
Assume I have the following model:
class ProductImage(models.Model):
image = models.ImageField('Product image', null=True,blank=True)
view = models.CharField(max_length=2, choices=VIEW_TYPES, default='FR', null=True,blank=True)
description = models.CharField(max_length=125, blank=True, null=True)
def __str__(self):
return self.view + "-" + self.image.url
I have the following queryset:
allProductImages = ProductImage.objects.all()
How do I extract the MEDIA_URL + path + filename for all objects (e.g. /media/image.jpg)
here's what I have already tried (follows on from previous queryset statement): urlValues = allImages.values('image')
<QuerySet [{'image': 'greenSwatch.jpg'}, {'image': 'blueSwatch.jpg'}, {'image': '3037-outer-black.jpg'}, {'image': '3037-outer-black.png'}]
I'm trying to get the following: <QuerySet [{'image': '/media/path/greenSwatch.jpg'}, {'image': '/media/path/blueSwatch.jpg'}, {'image': '/media/path/3037-outer-black.jpg'}, {'image': '/media/path/3037-outer-black.png'}, {'image': '/media/path/3037-inner-blue.png'}, {'image': '/media/path/3037-outer-green.png'}, {'image': '/media/path/3037-outer-blue.png'}]>
Also, I know there's no upload_to parameter specified, but this is optional according to the docs for Django 1.10
I'm running Django 1.10 + Python 3.5
p.image.url
gives you the full URL for the image, given p as a ProductImage instance. Since you have a queryset, you just need to iterate through; you shouldn't use values
though.
allProductImages = ProductImage.objects.all()
image_urls = [p.image.url for p in allProductImages]
Recommend
-
20
Django : Create a QuerySet from a list, preserving orderDjango : Create a QuerySet from a list, preserving order Fri 08 November 2013I thought it would be an easy one, but found myself lost with 34 opened tabs on stackoverflow
-
10
ImageField的使用笔记 今天完善作业写的订单系统,主要是给每一个菜品增加图片,看起来美观一些,但是没想到这个小小的需求花了我一天时间,记录下来,算增长知识了。 1.配置setting文件 MEDIA_ROOT代表的是上传图片的根目录,ME...
-
5
Django-filter and custom querysets Django-filter is a powerful tool, but the documentation is a little sparse. If you want to see examples of custo...
-
12
How would you tell Django to return ImageField.url (not relative path) when serializing a model? advertisements ImageField on the model
-
3
Controller to return an error message instead of a view? advertisements I'm fairly new to ASP.NET MVC and am not sure how best to handle the f...
-
5
How do I return a partial view based on the controller in the current page advertisements How do I render a partial view...
-
3
Django的view中常用return方法 2014-03-25编程技术 第一种HttpResponse,最基本的返回方式...
-
20
Methods that return new QuerySets¶ Django provides a range of
-
9
How to return view with data in Laravel 8 using Ajax 4091 views 7 months ago Laravel When sending data over Ajax in...
-
2
Cloudflare’s view of the Virgin Media outage in the UK 04/04/2023 Just after midnight (UTC) on April 4, subscribers to UK ISP Virgin Media (AS5089) began experienc...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK