Generating thumbnails from ImageField with easy-thumbnails


#1

I attempting to use easy-thumbnails in order to generate images from the ImageField. However the images are not showing up (pre-production).

I am wanting them to show up in the index.html view, using the image from the thing_detail.html view.

models.py

class Thing(models.Model):
...
    def get_image_path(instance, filename):
    return '/'.join(['article_images',instance.article.slug, filename])

class Upload(models.Model):
    article = models.ForeignKey(Article, related_name="uploads")
    image = models.ImageField(upload_to=get_image_path, blank=True)

index.html

{% extends 'layouts/base.html' %}
{% block title %}Home - {{ block.super }}{% endblock %}
{% load thumbnail %}
        
{% block content %}
    ...
   <img src="{{ MEDIA_URL }}{{ upload.image|thumbnail_url:'thumbnail' }}" class="media" alt=""/>
    ...
{% endblock %}

thing_detail.html

...    
{% for upload in uploads %}
     <img src="{{ upload.image.url }}" alt="" />
{% endfor %}
...

settings.py

...
THUMBNAIL_ALIASES = {
    '': {
        'thumbnail':{'size': (50, 50), 'crop': True},
    },
}
...

#2

I have also tried this with sorl-thumbnails - still no luck

index.html

 {% load thumbnail %}
 ... 
 {% for upload in thing.uploads.all %}
     {% thumbnail thing.image "200x200" as im %}
     <img src="{{ im.url }}" class="media" alt=""/>
     {% endthumbnail %}
  {% endfor %}
  ...

#3

What does the HTML look like? AKA, what does the link look like that it’s generating?


#4

Nothing shows up when it is run other than the content from base.html (my index.html is meant to display images as the link to the thing)


#5

Here is the index.html

Which when run shows nothing at all except for content from base.html.
It should look like this. (removed sorl.thumbnail forloop and set source to one from my pc. )


#6

Actually, I mean the rendered source — in your browser, you should be able to “view source” or something similar and see what Django renders. I would like to see what happens after Django takes your templates and creates HTML from them.


#7

Each thing is rendered as such from the source. Seems to skip the forloop all together. I get no errors at all.

    <a href="/things/test2/">
      

    <div class="caption">
      <div class="work_title">
        <h1>test2</h1>
      </div>
    </div>
    </a>
  </div>

#8

Sounds like a silent error… the thumbnail script is running into an issue and just does nothing rather than throwing an error. Any errors in your runserver output?


#9

This is the only error I get, it isn’t related I don’t think. The server still runs.

   /Users/friend/Projects/test/testapp/urls.py:44: RemovedInDjango19Warning: Default value of 'RedirectView.permanent' will change from True to False in Django 1.9. Set an explicit value to silence this warning.
      RedirectView.as_view(pattern_name='browse')),

#10

At this point I think you’ll have to talk to the plugin owners or ask on StackOverflow for help from users who are experienced with those plugins. Sorry I can’t help more. :(


#11

Hi friend, the reason you’re getting that warning is because Django is changing how it behaves when you use RedirectView (related docs here).

To fix it, you can add permanent=True to test/testapp/urls.py line 44 so it looks like this:

RedirectView.as_view(pattern_name='browse', permanent=True)),

#12

@limedaring No problem.

@shazow I’ll give that a try thanks!