1

I try to generate some thumbnails using Django 2 and the imagekit library.

When I want to get the thumbnails in templates, it returns me a image URL 404 error. The uploaded image is on the server, at the right place.

Here's my model :

class Article(models.Model):
    [...]
    uploaded_image = models.ImageField('Image miniature (upload)', upload_to='img', max_length=255, null=True, blank=True)
    optimized_image = ImageSpecField(source='uploaded_image', format='JPEG', options={'quality': 60, 'optimize':True})

My code in my template :

{% thumbnail '700x219' article.optimized_image -- alt=article.name %}

And my Nginx configuration settings for this website :

location /static/ {
    alias /home/user/myproject/site/static;
}

location /media/ {
    alias /home/user/myproject/site/media;
}

The static fils are serving correctly.

First, I found my generated thumbnails URL strange : https://www.mywebsite.com/media/CACHE/images/CACHE/images/img/my-img_ODSHuRU/b15da1962adc1c7a8031923bbf0fecb1/c2dd3587bf0c19faba27636f458188c3.jpg

It is repeating the /CACHE/images path...

So I watched at my Django's settings :

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR,'static'),
)

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

I don't see what I've done wrong. I probably failed my media directory configuration, or my Nginx configuration is not right too.

Is anybody has already had this trouble before ? Have you got some clues ?

Thanks for your help :)

Arthur C-G
  • 1,481
  • 4
  • 16
  • 23

1 Answers1

0

In your URL-s file add

urlpatterns = []+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Nginx:

location /static {
    alias /home/user/myproject/site/static;
}

location /media {
    alias /home/user/myproject/site/media;
}
Marin
  • 1,098
  • 14
  • 33
  • Thanks for your answer, I still have a 404 error on my media images. urlpatterns = [...] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) Should I remove something from my Nginx conf file ? – Arthur C-G Jul 20 '18 at 10:14
  • @Zoloom try with nginx conf – Marin Jul 20 '18 at 11:17