我正在尝试用 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 名声望...
还需要指定支持国际化的链接的文本(分别为“编辑”和“删除”)(针对链接内的文本)。
有人知道怎么做吗?
提前致谢