Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Django:在DataTables、ListView中显示图像_Javascript_Python_Jquery_Django_Datatables - Fatal编程技术网

Javascript Django:在DataTables、ListView中显示图像

Javascript Django:在DataTables、ListView中显示图像,javascript,python,jquery,django,datatables,Javascript,Python,Jquery,Django,Datatables,我想在jQuery DataTables中显示缩略图 类似的帖子显示,需要将js渲染函数添加到.DataTable设置中 我想在标准的基本情况下,使用Django,基于类的ListView实现这个答案 我的尝试创建了一个异常: AttributeError at /image/list/ 'ImageListView' object has no attribute 'refresh_from_db' Request Method: GET Request URL: //loc

我想在jQuery DataTables中显示缩略图

类似的帖子显示,需要将
js
渲染函数添加到
.DataTable
设置中

我想在标准的基本情况下,使用Django,基于类的ListView实现这个答案

我的尝试创建了一个异常:

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'
“ImageListView”对象没有“refresh\u from\u db”属性

下面是我的设置

表_settings.js views.py 表元素(在image_list.html中)
我找到了答案,并与大家分享了我自己努力寻找的答案。这要归功于@Mohit Rustagi在帖子中的回答

事实证明,使用基于类的ListView无需单独序列化数据,因为添加了
“render”:函数(数据、类型、行、元)
就足够了

我对上述问题做了以下修改:

更改“table_settings.js”
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')
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
    <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'
$('#imagetable').DataTable({
    "columns": [
    { "data": "product_image_name" },
    { "data": "product_image_description" },
    { "data": "product_image",
    "render": function(data, type, row, meta) {
                 return '<img src="/media/'+data+'" style="height:100px;"/>';}
}]},
);
class ImageListView(ListView):
    model = ProductImage
    template_name = 'product/image_list.html'