Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.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
Python django_表2不显示表元行属性_Python_Django_Django Tables2 - Fatal编程技术网

Python django_表2不显示表元行属性

Python django_表2不显示表元行属性,python,django,django-tables2,Python,Django,Django Tables2,我在项目中使用了django_tables2库,并试图实现一个表,其中每一行都有一个数据项名称属性。我的特定表继承自我的BaseTable类,而该类又继承自django_表2.table类。代码如下: page_table.py import django_tables2 as tables from django.utils.html import format_html from myapp.utils.base_table import BaseTable from myapp.mod

我在项目中使用了
django_tables2
库,并试图实现一个表,其中每一行都有一个
数据项名称
属性。我的特定表继承自我的
BaseTable
类,而该类又继承自
django_表2.table
类。代码如下:

page_table.py

import django_tables2 as tables

from django.utils.html import format_html

from myapp.utils.base_table import BaseTable
from myapp.models import MyModel

class PageTable(BaseTable):
    item_id = tables.Column(accessor='pk')

    # This method works perfectly fine as expected
    def render_item_name(self, record):
        return format_html(
            '<span data-item-id="{0}">{1}</span>'.format(
                record.item_id,
                record.item_name
            )
        )

    class Meta:
        model = MyModel
        # When debugging this attribute is accessed only on booting up
        # And is ignored when I for example first visit or refresh the page
        row_attrs={
            'data-item-name': lambda r: r.item_name
        }
        fields = (
            'item_id',
            'item_name',
            'item_description',
            'item_price', # etc
        )
from django_tables2 import Table

class BaseTable(Table):
    def __init__(self, *args, **kwargs):
        super(BaseTable, self).__init__(*args, **kwargs)
虽然表的呈现没有错误,也没有任何内容中断,但表中的一行上没有
数据项名称
属性,尽管就我所阅读的文档而言,在表的元类中声明此属性就足够了。是我弄错了还是我错过了什么?任何帮助都将不胜感激

另外,我正在使用Python2.7、Django 1.8.17和Django-tables2 v。1.16.0

Django表使用(以及Django)分解表类,这就是您在调试器中看到这些行仅在启动时命中的原因,因为是在创建表的类时

另一方面,当您自定义行属性时,django-tables2(按设计)具有行中当前数据的名称
record

您可以在中阅读:

默认情况下,为行提供了奇数和偶数类名,可以使用row_attrs Table.Meta属性或作为表的构造函数的参数来自定义这些行。将只添加类似字符串的值,使用可选的关键字参数调用callablesrecord,并添加返回值。比如说

Django表使用(以及Django)分解表类,这就是您在调试器中看到此类行仅在启动时被命中的原因,因为此时正在创建表的类

另一方面,当您自定义行属性时,django-tables2(按设计)具有行中当前数据的名称
record

您可以在中阅读:

默认情况下,为行提供了奇数和偶数类名,可以使用row_attrs Table.Meta属性或作为表的构造函数的参数来自定义这些行。将只添加类似字符串的值,使用可选的关键字参数调用callablesrecord,并添加返回值。比如说


由于某种原因,当我在lambda表达式中将
r
更改为
record
时,它正在工作。有人能给我解释一下吗?因为我不理解:(由于某种原因,当我将lambda表达式中的
r
更改为
record
时,它正在工作。有人能给我解释一下吗?因为我不理解:(谢谢,这解释了很多!我想我应该更仔细地阅读文档。谢谢,这解释了很多!我想我应该更仔细地阅读文档。