how to show thumbnail images from a local url stored in session system in Django?

237 views Asked by At

Django 3 + sorl-thumbnail 12.7.0

Hello! I'm using the session system to store various items like this:

  • request.session['cart'][1]
  • request.session['cart'][2]
  • ...
  • request.session['cart'][n]

In every session unit i want to store a product.image.url and other fields of the product:

        request.session.setdefault('cart', {})[str(request.session['counter'])] = {
            'producto': p.pk,
            'talla':    talla,
            'cantidad': cantidad,
            'producto_abstracto': p.producto_abstracto.pk,
            'imagen':      p.imagen_1.url,
            'nombre':      p.producto_abstracto.nombre_producto,
            'marca':       p.producto_abstracto.marca.marca,
            'precio_venta':p.producto_abstracto.precio_venta,
            'nombre_color':p.color_principal_id.nombre,
            'color':       p.color_principal_id.codigo,
        }

(I can't use pickleserializer to store in session the complete object Product for security concerns). Then I want to show in a view the product.image as a thumbnail but here is the problem. It seems that sorl-thumbnail just work with image Objects but i just have the url.

Then in template i try this:

{% load thumbnail %}

{% thumbnail '{{item.imagen}}' '300x300' as im %}
    <img class="im" style="width:100%;" src='{{ im.url }}'></img>
{% endthumbnail %}

As a result i have a 404 not found image with a route in cache like this: media/cache/31/a0/31a02cc7b19899a208a972a08e17fe12.jpg to a file that was not created.

As a comment when i use thumnail with classic django objects and images stored it works so i think it is not a problem about media and static routes or memcached.

What can i do to solve this problem and show thumbnail images from a url in session system?

1

There are 1 answers

0
andresGooz On

i finally solve it using sorl-thumbnail in views. When i was creating my session object i modified the way url was stored:

from sorl.thumbnail import get_thumbnail
request.session.setdefault('cart', {})[str(request.session['counter'])] = {
            'producto': p.pk,
            'talla':    talla,
            'cantidad': cantidad,
            'producto_abstracto': p.producto_abstracto.pk,
            'imagen':      get_thumbnail(p.imagen_1, '300x300').url,
            'nombre':      p.producto_abstracto.nombre_producto,
            'marca':       p.producto_abstracto.marca.marca,
            'precio_venta':p.producto_abstracto.precio_venta,
            'nombre_color':p.color_principal_id.nombre,
            'color':       p.color_principal_id.codigo,
        }

Now it works and the security system continues strong