django tables2创建带有链接的额外列 [英] django tables2 create extra column with links

查看:480
本文介绍了django tables2创建带有链接的额外列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的表:


我正在尝试为我的一个表添加一个额外的列,

  class ItemTable(tables.Table):
edit = tables.LinkColumn('item_edit',args = [A('pk')])
class Meta:
model =项目
fields =('name','slot','klass','rarity','price')

我的网址:

  url(r' ^ admin / item / edit /(?P< item_id> \d +)/ $',views.item_edit,name ='item_edit')

现在,我得到我的表,但最后一列(编辑)只有破折号+页面崩溃当我点击标题。



我一直在看 http:// django-tables2 .readthedocs.org / en / latest /#django_tables2.columns.LinkColumn ,我不知道我在哪里错了

决议案

您遇到的问题是由 LinkColumn 引起的,您希望绑定到您的项目 model,即它在你的实例上寻找一个 Item.edit 属性。



因为你实际上没有一个 Item.edit 属性,所以订购你的编辑列是没有意义的,你应该将其标记为不可顺序

  edit = tables.LinkColumn('item_edit',args = [A('pk')],orderable = False)

链接本身的文本来自 Item.edit 属性的值,您没有,因此您需要通过添加 render_edit 方法到你的表类:

  def render_edit(self):
return'Edit'

您可以替换'Edit' string随着你想要的disp



更新:根据@SunnySydeUp的建议,您还需要指定 empty_values =()对于列,为了得到其值:

  edit = tables.LinkColumn('item_edit',args = [A ('pk')],orderable = False,empty_values =())

参考:

http://django-tables2.readthedocs.org/en/latest/pages/order-by-accessors.html#specifying-alternative-ordering-for-a-column
http://django-tables2.readthedocs .org / en / latest / pages / custom-rendering.html#table-render-foo-methods



免责声明:此答案基于的django-tables2 文档和源代码,和不是招吨上的实际Django应用程序被测试。


I am trying to add a extra column to one of my tables, that adds url to another page.

My Table:

class ItemTable(tables.Table):
    edit = tables.LinkColumn('item_edit', args=[A('pk')])
    class Meta:
        model = Item
        fields = ('name', 'slot', 'klass', 'rarity', 'price')

my urls:

url(r'^admin/item/edit/(?P<item_id>\d+)/$', views.item_edit, name='item_edit')

Now with this, i get my table, but the last column (edit) only has dashes + the page crashes when i click the title.

i have been looking at http://django-tables2.readthedocs.org/en/latest/#django_tables2.columns.LinkColumn and im not sure where i go wrong

解决方案

The problems you've encountered are caused by LinkColumn expecting to be bound to a specific attribute in your Item model, i.e. it is looking for an Item.edit attribute on your instances.

Since you don't actually have an Item.edit attribute, ordering over your edit column makes no sense, and you should mark it as non-orderable:

edit = tables.LinkColumn('item_edit', args=[A('pk')], orderable=False)

The text of the link itself would come from the value of the Item.edit attribute, which you don't have, so you will need to provide it yourself by adding a render_edit method to your table class:

def render_edit(self):
    return 'Edit'

You can replace the 'Edit' string with whatever you want displayed in that column.

Update: As suggested by @SunnySydeUp, you also need to specify empty_values=() for the column, in order to get its value rendered:

edit = tables.LinkColumn('item_edit', args=[A('pk')], orderable=False, empty_values=())

References:
http://django-tables2.readthedocs.org/en/latest/pages/order-by-accessors.html#specifying-alternative-ordering-for-a-column http://django-tables2.readthedocs.org/en/latest/pages/custom-rendering.html#table-render-foo-methods

Disclaimer: This answer is based on the django-tables2 documentation and source code, and hasn't been tested on an actual Django application.

这篇关于django tables2创建带有链接的额外列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆