What is the difference between {% load staticfiles %} and {% load static %}

前端 未结 5 1626
说谎
说谎 2020-12-07 18:52

The most important part of the question is in the topic.

I am wondering what tag is best for which case. Moreover... I found code, that also use settings.STATI

5条回答
  •  春和景丽
    2020-12-07 19:18

    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:

    
    

    then, if I use {% load static %}, django throws 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.

提交回复
热议问题