Search code examples
djangodjango-admindjango-tables2

django-tables2 and LinkColumn. Correct path to Django-admin site page


I am developing cmdb application and trying to create a link to admin page of the device ( /admin/cmdb/device/device_id/) in django-tables2 LinkColumn with the following syntax:

id = tables.LinkColumn('admin:cmdb:device', args=[A('pk')])

This fails with error

NoReverseMatch at /cmdb/emp/171/

'cmdb' is not a registered namespace inside 'admin'

(/cmdb/emp/171/ - is the page on which the table is rendered)

How can I write the correct path in LinkColumn argument to Django admin page?


The goal could be achieved by using TemplateColumn:

id2 = tables.TemplateColumn('<a href="/admin/cmdb/device/{{record.id}}">{{record.id}}</a>')

but possibly someone could advise how to use LinkColumn?


Solution

  • Your question is not about the LinkColumn but about finding out the url names of the django admin pages.

    In any case, you can find your answer here: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#reversing-admin-urls

    So if the name of your application is cmdb and the name of your model is device, the url name of the device edit page would be admin:cmdb_device_change which could be used in the LinkColumn (also it can be used in the TemplateColumn using {% url "admin:cmdb_device_chang" record.id %}).