0

I am converting a small application by replacing handcrafted html-tables by django-tables2. But I am unable to add 2 linkify links (detail & update) towards separate pages.

Django 3.2.4 django-crispy-forms 1.12.0 django-filter 2.4.0 django-tables2 2.4.0

models.py
class Environment(models.Model):
    name = models.CharField(max_length=10)
    description = models.TextField(max_length=100, help_text="Enter a brief description")
    envType = models.CharField( max_length = 32, choices = ENVIRONMENT_TYPES,  default = "D" )

    def get_absolute_url(self):
        return reverse('environment-detail', args=[str(self.id)])

urls.py
urlpatterns = [
    path('', views.index, name='index'),
    path('environments/', views.EnvironmentList.as_view(), name='environments'),
    path('environment/<int:pk>', views.EnvironmentDetailView.as_view(), name='environment-detail'),
    path('environment/create/', views.EnvironmentCreate.as_view(), name='environment-create'),
    path('environment/<int:pk>/update/', views.EnvironmentUpdate.as_view(), name='environment-update'),
]

views.py
class EnvironmentList(PagedFilteredTableView):
    # based on https://stackoverflow.com/questions/25256239/how-do-i-filter-tables-with-django-generic-views
    model = Environment
    table_class = EnvironmentTable
    filter_class = EnvironmentFilter
    formhelper_class = EnvironmentListFormHelper         

forms.py
class EnvironmentListFormHelper(FormHelper):
    model = Environment
    form_tag = False
    # Adding a Filter Button
    layout = Layout('envType',  ButtonHolder(
        Submit('submit', 'Filter', css_class='button white right')
    ))

tables.py

class EnvironmentTable(dt2.Table):
    # https://github.com/jieter/django-tables2/commit/204a7f23860d178afc8f3aef50512e6bf96f8f6b
    name =dt2.Column(linkify = True) 
    edit = dt2.Column(default="edit",linkify=lambda record: record.get_update_url())
    #edit = dt2.LinkColumn('environment-update', args=[A('pk')])
    class Meta:
        model = Environment
        template_name = 'django_tables2/bootstrap-responsive.html'
        attrs = {
            'class': 'table table-bordered table-striped table-hover',
            'id': 'environmentTable'
            }
        fields = ("name", "description", "envType", "edit"  )
        per_page = 7          



environment_list.html
{% extends "base_generic.html" %}
{% load render_table from django_tables2 %}
{% load crispy_forms_tags %}

{% block content %}

<h1>Environment List</h1>
<br>
<a href="{% url 'environment-create' %}">Create a new environment</a>
<br>

<table border="2">
        <tr><th> Environment
      <th>Type 
      <th>Description
      <th>Action
      </tr>
  {% for object in environment_list %}
        <tr>
      <td><a href="{{ object.get_absolute_url }}"> {{ object.name }}</a> </td> 
      <td>{{object.envType}} </td> 
      <td>{{object.description}}  </td>  
      <td><a href="{% url 'environment-update' object.id %}">Update</a></td>
      </tr>
  {% endfor %}
</table>

<hr/>
<form action="" method="get"> 
    {% crispy filter.form filter.form.helper %}
</form>
{% render_table table 'django_tables2/bootstrap.html' %}

{% endblock content %}

This HTML contains the oldstyle handcrafted table : first column is a link towards the detail page, last column is a link to the update page.

The second section represents the django-tables2 table: but that table is lacking the link in the edit column. The information at https://github.com/jieter/django-tables2/commit/204a7f23860d178afc8f3aef50512e6bf96f8f6b had led me to a field definition of :

    edit = dt2.Column(default="edit",linkify=lambda record: record.get_update_url())

That would fetch the update url from

models.py
    def get_update_url(self):
        print ("getupdateurl")
        return reverse('environment-update', args=[str(self.id)])

Also following link column does not return a hyperlink

    edit = dt2.LinkColumn('environment-update', args=[A('pk')])

Any idea how I can resolve this?

Luc

1 Answers1

0

You can try this:

from django.urls import reverse_lazy
[...]
edit = dt2.Column(
    default="edit"
    linkify=lambda record: '{viewname}?id={id}'.format(
            viewname=reverse_lazy('enviroment:environment-update'),
            id=record.id
            )
)

just check if the url names are OK

regards

Joel Vega
  • 41
  • 3