Skip to main content

A Django template tag library which allows to download external images, store them locally and return the local path to locally stored image to a desired context variable

Project description

Fetch external images directly from templates.

A Django template tag library which allows to download external images, store them locally and return the local path to locally stored image to a desired context variable, along with width and height attributes of the image fetched. Caches the fetched images locally for the given time (set in settings).

You could, for example, use this app to solve the problems with displaying of a mixed content (assets loaded from HTTP and HTTPS sources).

Prerequisites

  • Django 1.8, 1.9, 1.10

  • Python >=2.7, >=3.4

Installation

  1. Install django-eximagination

Latest stable version on PyPI:

pip install django-eximagination

Latest stable version from GitHub:

pip install https://github.com/barseghyanartur/django-eximagination/archive/stable.tar.gz

  1. Add eximagination to INSTALLED_APPS.

INSTALLED_APPS = (
    # ...
    'eximagination',
    # ...
)
  1. Configure

By default, django-eximagination expects your files to be stored in /media/external_images directory. If location varies, redefine the directories in your Django settings, make sure the path is writable and that www-data (or whatever is applicable) has rights to write into it.

import os

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(BASE_DIR, '..', '..', 'media')
EXIMAGINATION_MEDIA_ROOT = os.path.join(MEDIA_ROOT, 'external_images')
EXIMAGINATION_MEDIA_URL = '/media/external_images'
EXIMAGINATION_MEDIA_RELATIVE_ROOT = 'external_images/'
# After 30 days we re-fetch the file anyway.
EXIMAGINATION_EXPIRATION_INTERVAL = 2592000

Usage example (in a Django template)

See the example directory for working code example.

Example #1:

{% load eximaginate %}

<img src="{{ MEDIA_URL }}{% eximaginate 'http://www.google.com/intl/en/images/logo.gif' %}">

Example #2:

{% load eximaginate thumbnail %}

{% eximaginate 'http://www.google.com/intl/en/images/logo.gif' as original %}

<img src="{% thumbnail original 100x100 %}">

In both cases there are two additional context variables added:

  • ei_width - Width of the image

  • ei_height - Height of the image

Testing

Simply type:

./runtests.py

or use tox:

tox

or use tox to check specific env:

tox -e py35

or run Django tests:

./manage.py test eximagination --settings=settings.testing

License

GPL 2.0/LGPL 2.1

Support

For any issues contact me at the e-mail given in the Author section or open an issue on BitBucket/GitHub.

Author

Artur Barseghyan <artur.barseghyan@gmail.com>

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-eximagination-0.8.2.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

django_eximagination-0.8.2-py2.py3-none-any.whl (13.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-eximagination-0.8.2.tar.gz.

File metadata

File hashes

Hashes for django-eximagination-0.8.2.tar.gz
Algorithm Hash digest
SHA256 755a48ce5b667c97fb30e5a0f634f4fb5f057c142c759f22073702fac1d38a11
MD5 5b275b190ecca89a0a8f7557eb7340c8
BLAKE2b-256 0b58e2e10fe12cfb5a658a549dac26a525ab11dcc35b2de3fff6872a33f98161

See more details on using hashes here.

File details

Details for the file django_eximagination-0.8.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_eximagination-0.8.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6859d77369cf4d2bc712b9ba2fc174668d99a2decc9c0b77bd58fd999cb99a11
MD5 a4d82ad16573ccdb198dab44200e1cd2
BLAKE2b-256 7f9141d88d55c0e2dec22caa4dabba188f49c61b4eb546ca58a34bcf77a9dbb5

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