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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With