Search code examples
pythondjangodjango-tables2

django-tables2 - no link generated in LinkColumn


I have added LinkColumn acording to documentation but no link is generated.

Moreover i have tried the aproach mentioned in django-tables2 linkcolumn multiple items in the same cell but without success.

Here is my code:

urls.py

....
url(r'user/edit/(?P<UserID>\d+)$', 'VMS.views.update_or_edit_user_profile', name='user-edit'),
url(r'user/edit/(\d+)$', 'VMS.views.update_or_edit_user_profile', name='user_edit'),
....

tables.py

import django_tables2 as tables
from models import UserProfile
from django.utils.safestring import mark_safe
from django.core.urlresolvers import reverse

class UserProfileTable(tables.Table):

    linkstest1 = tables.LinkColumn("user-edit", kwargs={"UserID": tables.A("pk")})



    class Meta:
        model = UserProfile
        # add class="paleblue" to <table> tag
        attrs = {"class": "paleblue"}

    column_name = tables.Column()


    def render_column_name(self, record):
        edit_url = reverse("user-edit", kwargs={"UserID": record.pk}, args=[record.pk])
        return mark_safe('''<a href="%s" class="tbl_icon edit">Edit</a>''' % (edit_url))

view:

def UserProfileList(request,template='User_List.html'):
    Userprofiles = UserProfile.objects.select_related().all()
    table = UserProfileTable(Userprofiles)
    RequestConfig(request, paginate={"per_page": 25}).configure(table)

    return render(request, template, {'table': table})

and template:

{% extends "base.html" %}
{% load render_table from django_tables2 %}

{% block content %}
     {% render_table table %}
{% endblock %}

The only thing generated is a "—" inside the table. No link, no error message, nothing.

Any ideas?

django-tables2 0.15.0 and django 1.6.5


Solution

  • For columns that are not part of your model, you need to add the empty_values attribute.

     linkstest1 = tables.LinkColumn("user-edit", kwargs={"UserID": tables.A("pk")}, empty_values=())
    

    Doc