Skip to main content

Lazy srcset and responsive image generation for Django. Minimum effort required. No database required.

Project description

PyPI Package Version MIT License PyPI Status Test Coverage

Python Versions Supported Django Versions Supported

Code Style Black PyPI Downloads GitHub Repo Stars

Django Lazy srcset

Lazy srcset and responsive image generation for Django. Minimum effort required. No database required.

Django Lazy srcset will create the markup and generate the images you need to provide responsive images via the srcset and sizes attributes for the img tag. All you need to do is install it, configure your breakpoints and use the {% srcset %} template tag.

All of the hard work (image generation and cacheing) is done by django-imagekit, by default this means images are generated just in time - lazily. Please see the django-imagekit docs for more info and configuration options.

Width and height attributes are generated to help avoid layout shifts. Make sure you set these in CSS so they are only used by the browser for aspect ratio.

SVG images are supported, they will not be converted or resized but width and height attributes are still added as well as the role="img" attribute.

You will also need Django and Pillow.

Installation & Usage

Install with pip:

$ pip install django-lazy-srcset

Add "imagekit" and "lazy_srcset" to INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    "imagekit",
    "lazy_srcset",
    # ...
]

Configure your breakpoints and stuff (most/all of this is optional):

# When disabled src, width and height attributes are returned so images still work but no srcset or image
# generation will happen. By default, lazy-srcset is disabled when debug is True.
LAZY_SRCSET_ENABLED = not django_settings.DEBUG

# The default threshold to use when not specified in the config.
LAZY_SRCSET_THRESHOLD = 69

# The default generator to use when not specified in the config.
LAZY_SRCSET_GENERATOR_ID = "lazy_srcset:srcset_image"

LAZY_SRCSET = {
    "default": {
        # breakpoints is the only setting you must define
        "breakpoints": [1920, 1580, 1280, 1024, 640],
        # If max_width is not provided the source image width is used.
        # It's a good idea to set this.
        "max_width": 2560,
        # If quality is not provided PIL will choose a default
        "quality": 91,
        # If format is not provided the source image format is used
        "format": "WEBP",
        # The difference in width (px) required to generate a new image
        # This prevents images being created which are too similar in size
        "threshold": LAZY_SRCSET_THRESHOLD,
        # If generator_id is not provided LAZY_SRCSET_GENERATOR_ID is used
        "generator_id": LAZY_SRCSET_GENERATOR_ID,
    }
}

Use the {% srcset %} template tag:

{% load lazy_srcset %}

{# image is an ImageField. With no args or kwargs provided all sizes are assumed to be 100vw #}
<img {% srcset image %} alt="Lovely and lazy" />

{# You can provide a path to a static file instead of an ImageField. #}
<img {% srcset 'path/to/my/image.png' %} />

{# You can provide relative (vw) image widths e.g. for a 4 - 3 - 2 - 1 col degradation #}
{# These are used to calculate the width of the generated images for each breakpoint #}
<img {% srcset image 25 33 50 %}  />

{# Kwargs can be used to define breakpoints and some settings #}
<img {% srcset image 1920=25 1580=33 1024=50 640=100 max_width=1920 %} />

{# Fixed width images can be defined with px units. These are always made regardless of threshold #}
<img {% srcset image '500px' '400px' '300px' %} />

Whilst not required it is advisable to take a nap at this stage.

For further documentation and examples of all the options please see the huge and obvious docstring in the source code for lazy_srcset/templatetags/lazy_srcset.py.

How it Works

If the source image is wider than the max_width it is resized to max_width. Otherwise it is converted as is. This image is then used in the srcset and src attributes.

For each breakpoint we calculate the target width of the image required and iterate from biggest to smallest. Images are then generated if the required width is:

  • Smaller than the source width (no upscaling!).

  • Smaller than the previously generated image by more than threshold px unless the size was defined with px units.

Once imagekit has generated an image it won’t create it again and it will store this fact in the cache to further speed up subsequent renders.

Advanced

Due to the awesomeness of django-imagekit it’s possible to configure django-lazy-srcset to use any image generator you have registered on a per config basis. Take a look at lazy_srcset/conf.py to see how to change the generator_id setting. For an example image generator look at lazy_srcset/imagegenerators.py. This is completely optional but I thought I’d mention it as there are potential artistic uses here; for example you could use a generator to add filters to some images.

Currently imagekit SourceGroup has not been implemented therefore the imagekit generateimages management command will not generate images for django-lazy-srcset. If you want to pre-generate images you can render_to_string() your templates in an appropriate save method or signal. If you are using django-content-blocks this happens on publish anyway.

Clean up of old, unused files created by django-lazy-srcset is down to you, if you require it at all.

Development Status & Roadmap

Django lazy srcset is in beta. There are currently no plans for further development.

Dependencies & Thank You

Other Packages to Consider

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-lazy-srcset-1.0.2.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

django_lazy_srcset-1.0.2-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

Details for the file django-lazy-srcset-1.0.2.tar.gz.

File metadata

  • Download URL: django-lazy-srcset-1.0.2.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for django-lazy-srcset-1.0.2.tar.gz
Algorithm Hash digest
SHA256 ed8bd8ffb87aefc4cd1264b95d28ba46466892668592d70eb7f4db11299da7f2
MD5 3f2a82a4f50e5666f2623f4f5a99989d
BLAKE2b-256 1b80be6970a6013c1e0850eaf408ecf43e5cc97803be64189c9a094b6f0cfd3d

See more details on using hashes here.

File details

Details for the file django_lazy_srcset-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_lazy_srcset-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 15c506a2192f7ff5708db388e21445b24e83477ebf8f2c2ba8d950e5083dd00b
MD5 838244b4825db6e11efb2f30ea68efe5
BLAKE2b-256 86d289d5db3452268ea62eecceff9b28f812137d6b7b4af9220dc4009740e340

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page