Reputation: 587
I've the following problem: In my models.py I'm using a Wagtail Image like this:
class ArtWork(models.Model):
image = models.ForeignKey(
'wagtailimages.Image',
on_delete=models.CASCADE,
related_name='+'
)
...
...
...
I know i can use the Image in my templates now like this:
{% image artwork.image height-100 %}
But for my current project I need to use the Image tag in my views.py because I'd like to generate a Pdf with it.
by simple using artwork.image
just the Title of the Image returns but I'd need something like:
<img alt="SomeName" src="/media/images/SomeName_155x200.max-1200x1200_22b3pND.height-100.jpg" width="77" height="100">
Also artwork.image.url
gives no result.
How can I use Wagtail Images outside my templates?
Upvotes: 1
Views: 1916
Reputation: 700
As noted by user Shacker here are the docs: https://docs.wagtail.org/en/stable/advanced_topics/images/renditions.html#image-renditions
Example:
# in your models
from wagtail.core.models import Page
class MyCustomPageModel(Page):
photo = models.ForeignKey(
'wagtailimages.Image',
...
# in your view
from myapp.models import MyCustomPageModel
p = MyCustomPageModel.objects.all()[0]
r = p.photo.get_rendition('fill-300x150')
print(r.url)
Upvotes: 0
Reputation: 696
If you need to get an URL of rendition (thumbnail), you can do the following:
artwork_page_object.image.get_rendition('height-100').url
or to access rendition file:
artwork_page_object.image.get_rendition('height-100').file
Instead of height-100
you can use any valid resizing method for the image
template tag (see the documentation for the image
tag)
If you want to access original image you can do it like:
artwork_page_object.image.file
Useful docs:
Upvotes: 7