A Django app that automates thumbnail creation for image fields.
Project description
Django Advance Thumbnail
Django Advance Thumbnail is a Django app that automates thumbnail creation for image fields. It generates, updates, and deletes thumbnails based on the source image, and allows custom thumbnail sizes.
Installation
- Install the package using pip:
pip install django_advance_thumbnail
Note: Starting from version 1.1.0, this package uses the modern
pyproject.tomlconfiguration instead of the legacysetup.cfg. See MIGRATION_GUIDE.md for details.
- Add
django_advance_thumbnailto yourINSTALLED_APPSinsettings.py:
INSTALLED_APPS = [
# ...
'django_advance_thumbnail',
# ...
]
Usage
Here's a basic example of how to use the AdvanceDJThumbnailField in a model:
from django.db import models
from django_advance_thumbnail import AdvanceThumbnailField
class MyModel(models.Model):
image = models.ImageField(upload_to='images/', null=True, blank=True)
thumbnail = AdvanceThumbnailField(source_field='image', upload_to='thumbnails/', null=True, blank=True,
size=(300, 300))
In this example, AdvanceDJThumbnailField is used to create a thumbnail from the image field. Whenever an image is
uploaded or updated, a corresponding thumbnail is automatically generated and stored in the thumbnail field. The
thumbnail's dimensions are determined by the optional size parameter, which defaults to (300, 300) if not specified.
This setup ensures that the lifecycle of the thumbnail is tied to its source image. If the source image is deleted, the associated thumbnail is also removed. This seamless synchronization simplifies image management in your Django models.
Advanced Features
Management Commands
Generate Thumbnails for Existing Images
When adding AdvanceThumbnailField to existing models in production:
# Generate thumbnails for all models
python manage.py generate_thumbnails
# Generate for specific model
python manage.py generate_thumbnails --model myapp.MyModel
# Force regenerate existing thumbnails
python manage.py generate_thumbnails --force
Regenerate Thumbnails After Size Changes
When you change the size parameter:
# Regenerate thumbnails with new sizes
python manage.py regenerate_thumbnails
# Force regenerate all thumbnails
python manage.py regenerate_thumbnails --force
Automatic Size Change Detection
The field automatically detects when thumbnail sizes change and regenerates them accordingly:
class MyModel(models.Model):
image = models.ImageField(upload_to='images/', null=True, blank=True)
thumbnail = AdvanceThumbnailField(
source_field='image',
upload_to='thumbnails/',
null=True,
blank=True,
size=(400, 400) # Changed from (300, 300) - will auto-regenerate
)
Smart Regeneration
By default, thumbnails are only regenerated when:
- The source image changes (detected automatically)
- The thumbnail size parameter changes
- The thumbnail doesn't exist
thumbnail = AdvanceThumbnailField(
source_field='image',
upload_to='thumbnails/',
null=True,
blank=True,
size=(300, 300) # Efficient - only regenerates when needed
)
Force Regeneration (Override)
Use force_regenerate=True only if you need to regenerate thumbnails on every save (not recommended for production):
thumbnail = AdvanceThumbnailField(
source_field='image',
upload_to='thumbnails/',
null=True,
blank=True,
size=(300, 300),
force_regenerate=True # Regenerates on every save - use sparingly
)
For detailed usage instructions, see the Usage Guide.
Contact
For any questions or feedback, feel free to reach out:
- Email: mh@mahadihassan.com, me.mahadi10@gmail.com
- Github: @itsmahadi007
- Linkedin: Mahadi Hassan
- Web: mahadihassan.com
Credits
This package was created by Mahadi Hassan. Special thanks to the Django and Python communities for their invaluable resources and support.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_advance_thumbnail-1.1.0.tar.gz.
File metadata
- Download URL: django_advance_thumbnail-1.1.0.tar.gz
- Upload date:
- Size: 22.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a9a83e66478d89c7add723434a7737e433fa0b75654543ddf24ded338f0ddf72
|
|
| MD5 |
75c50f18e0f07bcddf3cdcc7c3a459b3
|
|
| BLAKE2b-256 |
9117915222e72e781e097f7c28367707d36d1b64877ca42756a75899141224ea
|
File details
Details for the file django_advance_thumbnail-1.1.0-py3-none-any.whl.
File metadata
- Download URL: django_advance_thumbnail-1.1.0-py3-none-any.whl
- Upload date:
- Size: 44.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a32d39c3d0db80654913c974bb00f5bd4ee14cd35205a80f120ee333182ee487
|
|
| MD5 |
686c9540113d7f7b877c0379f269498c
|
|
| BLAKE2b-256 |
caadb4228036a1c069a3f25d216c4d8ea164b5b562156cc7583fc09565dd4b13
|