Search code examples
pythondjangoimageblockimagefield

Invalid block tag expected 'empty' or 'endfor'. Error trying to showing a ImageField


im tryinh to use the ImageField from pillow library. And i have a problem with that.

When i try to show my image, i get the error:

TemplateSyntaxError at /home Invalid block tag on line 62: 'articulo.nombre_imagen.url', expected 'empty' or 'endfor'. Did you forget to register or load this tag?

this is my line 62:

<a href="article/id/{{articulo.id}}"><img class="card-img-top" img src="{% articulo.nombre_imagen.url %}" alt=""></a>

as you can use i am using the syntax correctly, well i supossed...

this is from my models.py:

def upload_location(instance, filename):
return "static/img/" %(instance.id, filename)

class Articulo(models.Model):

nombre_imagen=models.ImageField(upload_to=upload_location,
    null=True, blank=True, 
    width_field="width_field", 
    height_field="height_field")
width_field=models.IntegerField(default=0)
height_field=models.IntegerField(default=0)

Anyone can help? Thank you!


Solution

  • Problem solved! if anyone wants to know the answer, i just add the function for i can show my actual image:

    def preload_image(request, pk)
    from .models import Articulo
    from django.http import HttpResponse
    from PIL import Image
    
    articulo = get_object_or_404(Articulo, pk=pk)
    
    img = Image.open(articulo.nombre_imagen.path)
    response = HttpResponse(content_type='image/%s' % img.format)
    img.save(response, img.format)
    response['Content-Disposition'] = 'filename="image.%s"' % img.format
    return response
    

    In my urls.py i added the url:

    url(r'^home', index, name='home'), it: url(r'^preload/(?P<pk>\d+)$', views.preload_image, name='preload_image'), 
    

    And finally i modify my line to use it with the function:

    <img class="card-img-top" src="{% url 'home:preload_image' pk=articulo.pk %}" alt="">
    

    thanks a lot @Sergey Rùdnev !