Django admin后台站点显示图片时,报了一个错误:The 'photo' attribute has no file associated with it.
原因:
数据中的一部分数据对象,并没有‘photo.url属性’
def show_img(self): img = mark_safe('<a href=%s><img src=%s></a>' % (self.photo.url, self.photo.url)) return img
解决方案:
在调用photo字段时,先做一次if判断photo字段是否存在和是否有‘url’的属性
def show_img(self): if self.photo and hasattr(self.photo, 'url'): img = mark_safe('<a href=%s><img src=%s width="100px"></a>' % (self.photo.url, self.photo.url)) return img
这个时候,再去访问admin后台站点,就不会报错了