简体   繁体   English

从字符串Django模板中删除第一个字符

[英]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:" }} 代码是{{ picture.picture_path|slice:"1:" }}

but it still comes out as ./DOF_mrD5T49.jpg . 但它仍然以./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 ? 是否有可能我无法将其删除,因为它是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. 您的第一次尝试不起作用的原因是picture.picture_path表示FieldFile对象而不是字符串。 This is what gets passed to the slice filter. 这是传递给slice过滤器的内容。

The slice filter fails silently if an invalid input is provided, and returns the original value that was supplied. 如果提供了无效输入,则slice过滤器将无提示失败,并返回提供的原始值。 It is only after this that Django attempts to convert that original value to a string, using the object's __str__ method. 只有在此之后,Django才会尝试使用对象的__str__方法将原始值转换为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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