How to add 2 additional LinkColumns to a table based on a model with django_tables2 in django 1.5

I am trying to make a table with 2 linkColumns with django-tables2, the links include the pk class of the model. Links point to 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 turn it on 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 does not show links. Each example (and questions) overrides the model column, but I need to add additional columns.

I cannot write messages because I need at least 10 reputations ...

table image

It is also necessary to specify the text of links (respectively, "Edit" and "Delete"), supporting internationalization (for text inside links).

Does anyone know how to do this?

Thank you in advance

+4
source share
2 answers

A small late answer, but I recommend removing the accounts: prefix from your table links as follows:

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

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

To show the links in the table, I added accessor = 'pk' in the column defination:

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

You should now have links with id numbers as text, which is not very good.

I did not find a way to specify the text of the Django-tables2 link, but with Jquery this is doable. To do this, add a class for your link as follows:

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

Then in your html add the following:

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

Hope this help!

0
source

Source: https://habr.com/ru/post/1483201/


All Articles