【发布时间】:2020-08-16 04:42:45
【问题描述】:
我想在 jQuery DataTables 中显示缩略图。
类似的帖子1 表明需要将js 渲染函数添加到.DataTable 设置中。
我想在标准的基本案例中实现这个答案,使用 Django,基于类的 ListView。
我的尝试创建了一个异常:
“ImageListView”对象没有属性“refresh_from_db”
下面是我的设置
table_settings.js
$('#imagetable').DataTable({
"data": "img_url",
'render': function (data, type, full, meta) {
return '<img src="'+data+'" style="height:100px;width:100px;"/>';}
});
models.py
class ProductImage(models.Model):
product_image_name = models.CharField(max_length=20)
product_image_description = models.TextField(max_length=255, null=True)
product_image = models.ImageField(default='product_image/default.jpg', upload_to='product_image')
views.py
class ImageListView(LoginRequiredMixin, ListView):
model = ProductImage
template_name = 'product/image_list.html'
image_url = ProductImage.product_image
def get_context_data(self, **kwargs):
data = super().get_context_data(**kwargs)
data['img_url'] = self.image_url
return data
表格元素(在 image_list.html 中)
<table id="imagetable">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Image</th>
</tr>
</thead>
<tbody>
{% for object in object_list %}
<tr>
<td> {{ object.product_image_name }} </td>
<td> {{ object.product_image_description }} </td>
<td> {{ object.product_image }} </td>
</tr>
{% endfor %}
</tbody>
</table>
以上产生异常:
AttributeError at /image/list/
'ImageListView' object has no attribute 'refresh_from_db'
Request Method: GET
Request URL: //localhost/image/list/
Django Version: 2.1.7
Exception Type: AttributeError
Exception Value:
'ImageListView' object has no attribute 'refresh_from_db'
【问题讨论】:
标签: javascript python jquery django datatables