![](/img/trans.png)
[英]How to set an unspecified value to Meta class (How to create a template table class) django_tables2
[英]django_tables2 Table Meta row attributes are not displayed
我在我的项目中使用django_tables2
库,并尝试实现一个表,其中每一行都具有data-item-name
属性。 我的特定表继承自BaseTable
类,而BaseTable
类又继承自django_tables2.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
)
base_table.py
from django_tables2 import Table
class BaseTable(Table):
def __init__(self, *args, **kwargs):
super(BaseTable, self).__init__(*args, **kwargs)
尽管呈现的表没有错误且没有任何问题,但表的单行上没有data-item-name
属性,尽管据我所知,据说在表的元类中声明此属性足够。 我是否误解了,或者我有什么想念的? 任何帮助将不胜感激。
PS我正在使用Python 2.7,Django 1.8.17和django-tables2 v.1.16.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.