4

我正在尝试用 django-tables2 制作一个带有 2 个 linkColumns 的表,这些链接包括模型类的 pk。链接指向编辑和删除表单。

模型.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')

网址.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'),
)

视图.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();

表格.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 名声望...

表格图像

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

有人知道怎么做吗?

提前致谢

4

2 回答 2

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',)
于 2013-08-21T11:17:48.730 回答
0

我结合 Django-tables2 和 Jquery 解决了这个问题。

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

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

现在您应该有带有ID 号的链接作为文本,这不是一个好主意。

我没有找到通过 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>

希望这有帮助!

于 2014-01-05T17:42:59.820 回答