Include a href in results

Issue #20 resolved
Marckr P created an issue

Hello,

Firstly, thanks for the efforts of making this program and making it available, it's a great solution for Django! Secondly, I am not sure if this is the correct place for an inquiry: I could not find a mailing list or support forum...

I have a datatable which has clickable links in the table results. Before using datatables with server side processing, my template would create the link from the object's slug. How would I go about doing the same when using a table created by django-datatables-view?

Best, Marcks

Comments (3)

  1. Marckr P reporter

    I made a custom render_column definition in the class defined in my views.py. It is the below code. Note that my model name and url name are the same, except that the model was typed with a upper case first letter. Adjust the url reversal for your own typical case.

        def render_column(self, row, column):
            if hasattr(row, 'get_%s_display' % column):
                text = getattr(row, 'get_%s_display' % column)()
            else:
                try:
                    text = getattr(row, column)
                except AttributeError:
                    obj = row
                    for part in column.split('.'):
                        if obj is None:
                            break
                        obj = getattr(obj, part)
                    text = obj
            if text is None:
                text = self.none_string
    
            if text and row.slug:
                return '<a href="%s">%s</a>' % (reverse(str.lower(self.model.__name__), args=(row.slug,)), text)
            else:
                return text
    
  2. Maciej Wisniowski repo owner

    It can be done with renderer column or by defining own prepare_results function. Have a look at Readme and example in Another example of views.py customisation section

  3. Marckr P reporter

    Thanks, I redid it with a prepare_results function. It's much cleaner now and easier to use. Thanks for your help!

  4. Log in to comment