Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove first character from string Django template

I know this has been asked multiple times but the solution that everyone reaches (and the documentation) doesn't seem to be working for me...

Trying to remove first character

Code is {{ picture.picture_path|slice:"1:" }}

but it still comes out as ./DOF_mrD5T49.jpg. Trying to get of the leading dot. Is it possible that I can't remove it because it's a the "name" of picture_path?

Pertinent model code:

class Picture(models.Model):
    picture_path = models.ImageField(blank=True)

    def __str__(self):
        return self.picture_path.name
like image 1000
manchakowski Avatar asked Jun 24 '16 04:06

manchakowski


1 Answers

This should work:

{{ picture.picture_path.name|slice:"1:" }}

The reason your first attempt didn't work is that picture.picture_path represents a FieldFile object rather than a string. This is what gets passed to the slice filter.

The slice filter fails silently if an invalid input is provided, and returns the original value that was supplied. It is only after this that Django attempts to convert that original value to a string, using the object's __str__ method.

like image 106
solarissmoke Avatar answered Sep 28 '22 18:09

solarissmoke