Skip to main content

Lazy srcset and 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 image generation for Django. Minimum effort required. No database required.

Django Lazy srcset will create all the markup and images you need to provide responsive images via the srcset attribute. 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.

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:

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",
    }
}

Use the {% srcset %} template tag:

{% load lazy_srcset %}

{# image is probably an ImageField #}
<img {% srcset image %} alt="Lovely and lazy" />

{# You can also provide relative image widths e.g. for a 4 - 3 - 2 - 1 col degradation: #}
<img {% srcset image 25 33 50 %}  />

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

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

For further documentation and usage examples please read the docstrings in the source code for lazy_srcset/templatetags/lazy_srcset.py.

Due to the awesomeness of 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.

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 unused files created by django-lazy-srcset is down to you, if you require it at all.

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-0.2.2.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

django_lazy_srcset-0.2.2-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django-lazy-srcset-0.2.2.tar.gz
  • Upload date:
  • Size: 10.0 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-0.2.2.tar.gz
Algorithm Hash digest
SHA256 83df0f6c780b865c5970ec83c7f8795f9e8067680c3d4653ea5fdbfbca6e11ec
MD5 1e5b23cf99a7647933ca6813120fbbd9
BLAKE2b-256 a9c8b37adfbfaaa590f0fb5a3f2b0e840205d4e35408ab1e715d796910ebe4ec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_lazy_srcset-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2f0e7a518091e32b13de99fcc21cabe822b507771a19a1ac690a5458b6526795
MD5 acc4145a80df3be08497c250989cc831
BLAKE2b-256 cff6160751a69dfd152385458b1450a4523bc4d7f8cf9aade3e11b0bd0b666cc

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