django-versatileimagefield icon indicating copy to clipboard operation
django-versatileimagefield copied to clipboard

Pre-warm not working on Django with empty log

Open kuipumu opened this issue 6 years ago • 1 comments

Hi, I deployed my Django 2.2.2 project online, so I disabled the option to create images on demand and set the rendition keys for my images on the settings of my project, then I set a post save action to create all the images I need, but after the model is saved there is no images created by the post save. I followed the procedure that shows on the documentation of the app. I also tried to just work with the images on demand but every time I create a new object I just get a 500 error on my Apache server. What I am doing wrong?, here is the code I added to pre warm the images.

settings.py:

VERSATILEIMAGEFIELD_SETTINGS = {
    'cache_length': 2592000,
    'cache_name': 'versatileimagefield_cache',
    'jpeg_resize_quality': 70,
    'sized_directory_name': '__sized__',
    'filtered_directory_name': '__filtered__',
    'placeholder_directory_name': '__placeholder__',
    # Whether or not to create new images on-the-fly. Set this to `False` for
    # speedy performance but don't forget to 'pre-warm' to ensure they're
    # created and available at the appropriate URL.
    'create_images_on_demand': False,
    'image_key_post_processor': None,
    'progressive_jpeg': True
}

VERSATILEIMAGEFIELD_RENDITION_KEY_SETS = {
    'posten_thumbnail': [
        ('thumbnail_1_crop', 'crop__128x72'),
        ('thumbnail_2_crop', 'crop__384x216'),
        ('thumbnail_3_crop', 'crop__640x360'),
        ('thumbnail_4_crop', 'crop__512x288'),
        ('thumbnail_5_crop', 'crop__768x432'),
        ('thumbnail_6_crop', 'crop__1280x720'),
        ('thumbnail_7_crop', 'crop__1920x1080'),
        ('thumbnail_8_crop', 'crop__600x400'),
        ('thumbnail_9_crop', 'crop__800x600'),
        ('thumbnail_9_crop', 'crop__300x200'),
        ('thumbnail_10_crop', 'crop__1400x750'),
    ],
    'postes_thumbnail': [
        ('thumbnail_1_crop', 'crop__128x72'),
        ('thumbnail_2_crop', 'crop__384x216'),
        ('thumbnail_3_crop', 'crop__640x360'),
        ('thumbnail_4_crop', 'crop__512x288'),
        ('thumbnail_5_crop', 'crop__768x432'),
        ('thumbnail_6_crop', 'crop__1280x720'),
        ('thumbnail_7_crop', 'crop__1920x1080'),
        ('thumbnail_8_crop', 'crop__600x400'),
        ('thumbnail_9_crop', 'crop__800x600'),
        ('thumbnail_9_crop', 'crop__300x200'),
        ('thumbnail_10_crop', 'crop__1400x750'),
    ],
    'image_thumbnail':[
        ('thumbnail_1_crop', 'crop__128x72'),
        ('thumbnail_2_crop', 'crop__384x216'),
        ('thumbnail_3_crop', 'crop__512x288'),
        ('thumbnail_4_crop', 'crop__768x432'),
    ]
}

models.py

@receiver(models.signals.post_save, sender=Image)
def warm_Image_thumbnail_images(sender, instance, **kwargs):
    thumbnail_img_warmer = VersatileImageFieldWarmer(
        instance_or_queryset=instance,
        rendition_key_set='image_thumbnail',
        image_attr='thumbnail'
    )
    num_created, failed_to_create = thumbnail_img_warmer.warm()

@receiver(models.signals.post_save, sender=PostEN)
def warm_PostEN_thumbnail_images(sender, instance, **kwargs):
    thumbnail_img_warmer = VersatileImageFieldWarmer(
        instance_or_queryset=instance,
        rendition_key_set='posten_thumbnail',
        image_attr='thumbnail'
    )
    num_created, failed_to_create = thumbnail_img_warmer.warm()

@receiver(models.signals.post_save, sender=PostES)
def warm_PostES_thumbnail_images(sender, instance, **kwargs):
    thumbnail_img_warmer = VersatileImageFieldWarmer(
        instance_or_queryset=instance,
        rendition_key_set='postes_thumbnail',
        image_attr='thumbnail'
    )
    num_created, failed_to_create = thumbnail_img_warmer.warm()

When I run these post save signals it doesn't work, but these same codes work when I do it from Django shell.

kuipumu avatar Jul 31 '19 18:07 kuipumu

For OP or someone facing a similar question, maybe these notes will help.

  • if you have signals.py as its own module, make sure it gets imported somewhere (I do it in apps.py), otherwise the signal may not get registered (the code may never be evaluated)
  • make sure the post_save signal is actually being sent.
    • M2M saves do not send post_save
    • admin saves do not send signals
    • for more info on these, see the Django official docs.

And if you're wondering for an easy way to check what's going on with your signal hooks... I strongly recommend snoop/birdseye and using @spy all over the place (of course only enabled if DEBUG, not in prod; the snoop docs tell you how to do it right).

floer32 avatar Dec 23 '19 08:12 floer32