manchakowski
manchakowski

Reputation: 127

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

Upvotes: 6

Views: 11276

Answers (1)

solarissmoke
solarissmoke

Reputation: 31414

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.

Upvotes: 15

Related Questions