Skip to main content

An image utility that renders images lazily as well as intrinsically sizes them.

Project description

# 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) {
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)
...
})
```


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-image-0.2.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_lazy_image-0.2.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file django-lazy-image-0.2.0.tar.gz.

File metadata

File hashes

Hashes for django-lazy-image-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3a1a7bab30f82d20a2eec7eff7f00ac83d1480ff9dbb438d84b83f701a6062ea
MD5 4bf30680fc7f7c319d84896f31edc5e9
BLAKE2b-256 1a0a1e6e30bccce4a3ced9c3f34f4ff7cd737ae665da86d562a22fe432ef46e8

See more details on using hashes here.

File details

Details for the file django_lazy_image-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_lazy_image-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a92a5c4f6c20c2379a5655d081db9e8f6a05f5e118d0b823dd253e5a831fa0cb
MD5 2b75a08b1a5116a09e5b7486abfad5b4
BLAKE2b-256 4c105e31aa4aed66fb984c6635e261fa8d1ce4a1346a75164a4e9c95cdbfc3d8

See more details on using hashes here.

Supported by

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