Skip to main content
# Django Lazy Image

`django-lazy-image` is a small Django utility that allows you to render images lazily on the frontend to minimise initial page load.

## Installation

To install `django-lazy-image` just run:

```cli
pip install django-lazy-image
```

## Requirements

* Jinja2
* Django >= 1.11 (It's only been tested on 1.11 but i'm pretty sure it will work on 1.8+)
* [Django Jinja](https://github.com/niwinz/django-jinja)

## Configuration

Add `django_lazy_image` to your `INSTALLED_APPS`

```python
INSTALLED_APPS = [
...
'django_lazy_image',
...
]
```

Add `url(r'^assets/', include('django_lazy_image.urls', namespace='assets')),` to your base `urls.py`

```python
urlpatterns = [
...
url(r'^assets/', include('django_lazy_image.urls', namespace='assets')),
...
```

Then there are 2 ways to use the static files with Django:

### NPM (The preferred way)

You can simply do

```cli
npm install django-lazy-image
```

or

```cli
yarn add django-lazy-image
```

Then include the Javascript / CSS as a module

**file.js**
```javascript
import LazyImage from 'django-lazy-image';

// Only run the below code if the page has a lazy image instance
const lazyImage = document.querySelector('.js-LazyImage')
if (lazyImage) {
// Collect all the lazy images on the page
const lazyImages = document.querySelectorAll('.js-LazyImage')
// Set up our IntersectionObserver callback
const callback = (entries, observer) => {
Array.from(entries).forEach((entry, index) => {
// If any of the images have come in to view, activate them sequentially
if (entry.isIntersecting && !entry.target.dataset.activating) {
entry.target.dataset.activating = true
window.setTimeout(() => {
new LazyImage({ el: entry.target })
observer.unobserve(entry.target)
}, 150 * index)
}
})
}
const observer = new IntersectionObserver(callback, {
threshold: 0.4
})
Array.from(lazyImages).forEach(image => observer.observe(image))
}
```

**file.css**
```css
@import 'django-lazy-image/dist/css/lazy-image.css';
```

### Django Static
Somewhere in your `base.html` add:

```html
{{ static('django_lazy_image/css/lazy-image.css') }}
{{ static('django_lazy_image/js/lazy-image.js') }}
```

You can then use the `lazyImage` function that is on the global object

```javascript
var images = [].slice.call(document.querySelectorAll('.js-LazyImage'))
images.forEach(function (el) {
var image = window.lazyImage(el)
...
})
```


Metadata

Release files for django-lazy-image 0.5.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-lazy-image 0.5.2
File Size Uploaded
django-lazy-image-0.5.2.tar.gz 6.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-lazy-image 0.5.2
File Interpreter ABI Platform
django_lazy_image-0.5.2-py3-none-any.whl Python 3 none any Details

Total release size: 14.0 kB

Release files / django-lazy-image-0.5.2.tar.gz

Download URL django-lazy-image-0.5.2.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b5711bbf476998aeea65abce287233eae206f82a9b47305dbfeddb0678f0cd2e
BLAKE2b-256 checksum
How to use checksums
fc4be18dfc33cc80813988f84db30d3caed851135fcbba45360443f1e85a9ecb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_lazy_image-0.5.2-py3-none-any.whl

Download URL django_lazy_image-0.5.2-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
393a79b5f58a38b2c997d3048e561ea41f61b4596680a712f9b2db718e57514d
BLAKE2b-256 checksum
How to use checksums
195e67ce97cc3ec33607a4b39a7a845dfd124b88dc1d657160db7d951a59bd34
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.5.2 This release

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.18

2 release files

0.0.17

2 release files

0.0.16

2 release files

0.0.15

2 release files

0.0.13

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page