2015-10-17 78 views
1

我使用Django表2到列表中的項目,我想用一個超鏈接與所選行詳細信息使用添加一列到另一頁(編輯詳細信息頁面)其模型中的主鍵。
我已經在這裏作爲其工作(該鏈接打開的預期頁),但它正顯示出的itemid在列,而我希望把一個鏈接誰的顯示文本,標記爲「編輯」使用的鏈接欄。有什麼可以幫助我如何做到這一點?我想模仿<a href="myurl"> Edit </a>行爲如何更改顯示文本在Django表2鏈接列

# tables.py 
class MyItemsTable(tables.Table): 
    itemid = tables.LinkColumn('myapp.views.edit_item', 
         args=[A('pk')],orderable=False) 
    class Meta: 
     model = models.MyItems 
     attrs = {"class": "paleblue"} 

# views.py  
def myitems_list(request): 
    myitems = models.MyItems.objects.all() 
    table = tables.MyItemsTable(myitems) 
    RequestConfig(request).configure(table) 
    return render(request, 'myapp/mylist.html', {'table':table,'myitems':myitems}) 

def edit_item(request, pk): 
    selecteditem = get_object_or_404(models.MyItems, pk=pk) 
    return render(request, 'myapp/edit_item.html', {'selecteditem': selecteditem}) 

# models.py 
class MyItems(models.Model): 
    itemid = models.IntegerField(primary_key=True) 
    itemfullname = models.TextField('Full Name',blank=True, null=True) 
+0

[Django - django-tables2 LinkColumn的Overide數據內容]的可能重複(http://stackoverflow.com/questions/16198701/django-overide-data-content-of-a-django-tables2-linkcolumn) – Serafeim

+0

其實原來的解決方案是使用模板列如下http://stackoverflow.com/questions/13211301/django-tables2-linkcolumn-external-url – Avagut

+1

課程的模板列是* *顯而易見的解決方案。我在重複問題中提出的答案是* DRY *解決方案! – Serafeim

回答

0

你可能已經解決了這一點,但沒有添加自定義的文本,而不是使用TemplateColumn一個更好的辦法,而功能實際上已經內置於LinkColumn。要添加您的編輯字符串:

# tables.py 
class MyItemsTable(tables.Table): 
    itemid = tables.LinkColumn('myapp.views.edit_item', 
        args=[A('pk')], orderable=False, text='Edit') 
class Meta: 
    model = models.MyItems 
    attrs = {"class": "paleblue"} 

您可以將字符串動態任何你想要使用像text=lambda record: 'edit-{0}'.format(record.name)例如。看看documentation