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

时间:2013-05-28 16:36:40

标签: python django django-tables2

我正在尝试使用带有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

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

任何人都知道怎么做?

提前致谢

2 个答案:

答案 0 :(得分:2)

答案很少,但我建议您从表格链接中删除accounts:前缀,如下所示:

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

答案 1 :(得分:0)

我用Django-tables2本身和Jquery的组合解决了这个问题。

为了让表格显示链接,我在列定义中添加了 accessor ='pk'

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

现在您应该将 id number 作为文本链接,这不是一个好主意。

我没有找到通过Django-tables2指定链接文本的方法,但是使用Jquery这是可行的。为此目的,为您的链接添加类,如下所示:

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

然后,在你的html中,添加:

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

希望这有帮助!