python – 如何在django 1.5中使用django_tables2在基于模型的表中添加2个额外的LinkColumns

我正在尝试使用带有
django-tables2的2个linkColumns来创建一个表,这些链接包含模型类的p​​k.

链接指向编辑和删除表单.

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')

这不是绝对必要的,但我包括它以防万一

模板/帐号/ 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

模板/帐号/ 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>

我不知道为什么表没有显示链接.
每个示例(和问题)都会覆盖模型列,但我需要添加额外的列.

我无法发布图片因为我需要至少10个声望…

table image

还需要指定支持国际化的链接文本(分别为“编辑”和“删除”)(对于链接内的文本).

有人知道怎么做吗?

提前致谢

最佳答案 回答很少,但我建议删除帐户:来自表格链接的前缀如下:

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