django static Cannot mix str and non-str arguments

I don't know what the difference is supposed to be, but I found a use case difference (using django 1.9.1 running via apache, wsgi on Python 3.4). In my app, I have some images in ImageFields in the database. If I use code like this in my template:

<a href="object-{{object.id}}"><img src="{% static object.image %}" height="200px"></a>

then, if I use {% load static %}, django thorws a TypeError (Cannot mix str and non-str arguments). This is presumably because the object.image is not a string, it's an ImageField, that gets converted to a string at some later stage. However, if one uses {% load staticfiles %} no such error occurs.

Unfortunately, I discovered this difference after spending hours trying to debug the problem. I managed to find a workaround for when using the first option, namely to add a string-converter method to the object like this:

#image string
def image_str(self):
    return str(self.image)

Hope this knowledge will be of use to someone.

原链接:https://stackoverflow.com/questions/24238496/what-is-the-difference-between-load-staticfiles-and-load-static

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值