About Generating Image Versions in Wagtail

Wagtail has two approaches on generating image versions on demand:

  • {% image %} template tag generates the image version on page load.
  • {% image_url %} generates a dynamic URL of the view that generates an image version.

For performance reasons, it's beneficial to always use {% image_url %}–the main document will render as soon as possible, and the images will be generated and retrieved for each usage as separate requests.

urls.py

from wagtail.images.views.serve import ServeView

urlpatterns = [
    re_path(
        r"^images/([^/]*)/(\d*)/([^/]*)/[^/]*$", 
        ServeView.as_view(),
        name="wagtailimages_serve",
    ),
    re_path(r"", include(wagtail_urls)),
]

header.html

{% load wagtailimages_tags %}
{% image_url page.cover_image "width-800" as img_url %}
<img src="{{ img_url }}" alt="{{ page.cover_image.title }}" width="800" />

Tips and Tricks Programming Development Django 5.2 Django 4.2 Wagtail 7