简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM