从字符串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...
尝试删除第一个字符
代码是 {{picture.picture_path | slice:"1:"}}
,但仍显示为 ./DOF_mrD5T49.jpg
.试图获得领先点.我可能无法删除它,因为它是 picture_path
的名称"吗?
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
?
相关型号代码:
class Picture(models.Model):
picture_path = models.ImageField(blank=True)
def __str__(self):
return self.picture_path.name
推荐答案
这应该有效:
{{ picture.picture_path.name|slice:"1:" }}
您第一次尝试失败的原因是 picture.picture_path
表示 FieldFile
对象而不是字符串.这就是传递给 slice
过滤器的内容.
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.
如果提供了无效的输入,则 slice
过滤器将以静默方式失败,并返回提供的原始值.只有在此之后,Django才会尝试使用对象的 __ str __
方法将该原始值转换为字符串.
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.
这篇关于从字符串Django模板中删除第一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!