Question

I am trying to do a table with 2 linkColumns with django-tables2, the links include the pk of the model class. The links point to the Edit and Delete forms.

models.py

from django.db import models
from django.utils.translation import ugettext_lazy as _

class Person(models.Model):
    first_name = models.CharField(_('first name'), max_length=200, blank=False)
    last_name = models.CharField(_('last name'), max_length=200, blank=False)

   class Meta:
       verbose_name = _('person')
       verbose_name_plural = _('persons')

urls.py

from django.conf.urls import patterns, url
from accounts import views

urlpatterns = patterns('',
    url(r'^person/list/$', views.PersonList.as_view(), name='person_list'),
    url(r'^person/add/$', views.PersonAdd.as_view(), name='person_add'),
    url(r'^person/(?P<pk>\d+)/update/$', views.PersonUpdate.as_view(), name='person_update'),
    url(r'^person/(?P<pk>\d+)/delete/$', views.PersonDelete.as_view(), name='person_delete'),
)

views.py

from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.core.urlresolvers import reverse_lazy
from django_tables2 import SingleTableView
from accounts.tables import PersonTable
from accounts.models import CreditCardTransaction, Person

class PersonAdd(CreateView):
    model = Person
    template_name = 'accounts/person_form.html'
    success_url = reverse_lazy('accounts:person_list')

class PersonDelete(DeleteView):
    model = Person
    template_name = 'accounts/person_form.html'
    success_url = reverse_lazy('accounts:person_list')

class PersonUpdate(UpdateView):
    model = Person
    template_name = 'accounts/person_form.html'
    success_url = reverse_lazy('accounts:person_list')

class PersonList(SingleTableView):
    model = Person
    template_name = 'accounts/person_list.html'
    table_class = PersonTable
    def get_table_data(self):
        return Person.objects.all();

tables.py

import django_tables2 as tables
from django_tables2.utils import A
from accounts import models

class PersonTable(tables.Table):
    edit_link = tables.LinkColumn('accounts:person_edit', args=[A('pk')], verbose_name='edit',)
    delete_link = tables.LinkColumn('accounts:person_delete', args=[A('pk')], verbose_name='delete',)

    class Meta:
        model = models.Person
        attrs = {"class": "paleblue"}
        fields = ('first_name', 'last_name', 'edit_link', 'delete_link')

this is not strictly necessary, but I include it just in case

templates/accounts/person_list.html

{% load render_table from django_tables2 %}
{% load static %}
<!DOCTYPE HTML>
<html>
<head>
    <link rel="stylesheet" href="{% static 'django_tables2/themes/paleblue/css/screen.css'%}" />
</head>
<body>
   <p>
       <a href="{% url 'accounts:person_add' %}">Add</a>
   </p>
    <p>
       {% render_table table %}
    </p>
</body>
</html

templates/accounts/person_form.html

<!doctype HTML>
<html>
<head>
</head>
<body>
    <form action="." method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <input type="submit" name="add" value="Add">
    </form>     
</body>
</html>

I do not know why the table is not showing the links. Every examples (and questions) override a model column, but I need to add extra columns.

I cant post images because I need at least 10 reputation...

table image

Also need to specify the text of the links ('Edit' and 'Delete' respectively) supporting internationalization (for the text inside the links).

Anyone know how to do this?

Thanks in advance

Was it helpful?

Solution

Little late answer but I recommend removing the accounts: prefix from your table links like this:

edit_link = tables.LinkColumn('person_edit', args=[A('pk')], verbose_name='edit',)
delete_link = tables.LinkColumn('person_delete', args=[A('pk')], verbose_name='delete',)

OTHER TIPS

I solved this problem with a combination of Django-tables2 itself and Jquery.

In order to have the table show links, I added accessor='pk' in the column defination:

edit_link = tables.LinkColumn('accounts:person_edit', args=[A('pk')], verbose_name='edit', accessor='pk')

Now you should have links with id numbers as the text, which is not a good idea.

I did not find a way to specify the link text by Django-tables2, but using Jquery this is doable. For this aim, add class for your link like this:

edit_link = tables.LinkColumn('accounts:person_edit', args=[A('pk')], verbose_name='edit', accessor='pk', attrs={"class": "edit_link"})

Then, in your html, add this:

<script type="text/javascript">
    $(document).ready(function(){
        $(".edit_link").text('Edit');
    });
</script>

Hope this help!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top