3

I have a model with some fields and I want to add a LinkColumn to a detail page. I have a working version, but I want to move to django-tables2

The problem is that the link column doesnt show any link, just a "-"

The model

class Events(models.Model):
    id = models.IntegerField(primary_key=True)
    date = models.DateField(null=True, blank=True)
    time = models.TimeField(null=True, blank=True)

The Table. Here I tried with args=[A('id')] and args=[A('pk')]

class EventsTable(tables.Table):

    time = tables.TemplateColumn("{{value|time:'H:i'}}", verbose_name='Time UTC')

    detail_link = tables.LinkColumn('detail', args=[A('id')], verbose_name='Detail')

    class Meta:
        model = Events
        attrs = {"class": "paleblue"}

        fields = ("date", "time", "detail_link")

mi url pattern is

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^(?P<event_id>\d+)/$', views.detail, name='detail'),
)

and the view

def index(request):
    table = EventsTable(Events.objects.all(), order_by=('-date', '-time'))
    RequestConfig(request, paginate={"per_page": PAGE_LIMIT}).configure(table)
    return render(request, "db_interface/events.html", {"table": table})

EDIT: Changing the detail_link to

detail_link = tables.LinkColumn('detail', args=[A('id')], verbose_name='Detail', empty_values=())

now I got a NoReverseMatch Exception

Reverse for 'detail' with arguments '(5075,)' and keyword arguments '{}' not found

the number 5075 is the id of the first event. I dont know if for any reason is not passing the argument as an int ?

Pablo V.
  • 324
  • 2
  • 16

1 Answers1

3

Try:

detail_link = tables.LinkColumn('detail', args=[A('id')], verbose_name='Detail', empty_values=())

According to the docs, render methods are only called if the value for a cell is determined to be not an empty value. Since the Event model does not have a detail_link field, there's no value given to it.

SunnySydeUp
  • 6,680
  • 4
  • 28
  • 32
  • Thanks, I tried and now I got an error. `Reverse for 'detail' with arguments '(5075,)' and keyword arguments '{}' not found.` 5075 is the id of the firs event. The error appears at `{% for column, cell in row.items %} ` I suppose is a problem with the url but i cant figure it out – Pablo V. Dec 16 '13 at 13:58
  • Does it work if you do reverse("detail", args=[event.id]) in the shell or view? – SunnySydeUp Dec 16 '13 at 22:01
  • if I put the url in the browser it works ok, (http://127.0.0.1:8000/db_interface/5075/ , but not in the shell. Before using django-tables, i used the template code `` which works – Pablo V. Dec 17 '13 at 13:29
  • ah, so the url was namespaced. So if you do reverse("db_interface:detail", args=[event.id]), it should work? If it does, replace "detail" in the `LinkColumn` with "db_interface:detail" – SunnySydeUp Dec 17 '13 at 22:01
  • yes, it works , thanks ! The only detail is that appears a 'None' string. The link is rendered this way `None`. i'm trying to change the none via attrs but It didnt work. At the end I need to put an image there, the solution I've found is to create a `CustomTextLinkColumn`. Do you have another idea?. Thanks Again – Pablo V. Dec 18 '13 at 12:25
  • 1
    Finally I just created a TemplateColumn and it works – Pablo V. Dec 18 '13 at 14:32