链接到DetailView时出现Django NoReverseMatch错误

时间:2014-03-26 16:12:19

标签: python django http django-tables2

从记者的主视图链接到我的DetailView时遇到问题。两个网址都可以。我知道这个问题有很好的记录,但URL很混乱,我似乎无法找到解决方案。 这是我的views.py,table.py的代码(错误是由于这里的一行),urls.py。

将非常感谢帮助。 链接是/ reporter /应链接到/ reporter / pk / detail `

tables.py 
    import django_tables2 as tables
from django_tables2.utils import A
from models import Reporter

class ReporterTable(tables.Table):
    #my error is on the next time. It works for #list_all but not detail. 
    id = tables.LinkColumn('mysite.apps.reporters.views.detail', args=[A('pk')])
    class Meta:
        model = Reporter
        attrs = {"class": "paleblue"}
        fields = ('id', 'surname', 'othername', 'gender')

class ReporterViewTable(tables.Table):
    class Meta:
        model = Reporter
        attrs = {"class": "paleblue"}

views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.template import RequestContext, loader
from django.views.generic.detail import DetailView
from django_tables2 import RequestConfig
from tables import ReporterTable, ReporterViewTable

from models import Reporter

class ReporterDetailView(DetailView):
    model = Reporter

    def get_context_data(self, **kwargs):
        context = super(ReporterDetailView, self).get_context_data(**kwargs)
        return context

def index(request):
    table = ReporterTable(Reporter.objects.all())
    RequestConfig(request).configure(table)
    return render(request, 'reporters/index.html', {'table':table})

urls.py

from django.conf.urls import patterns, include, url

from django.views.generic import DetailView, ListView
from views import ReporterDetailView

urlpatterns = patterns('mysite.apps.reporters.views',
    url(r'^$', 'index'), 
    url(r'^list_all/$', 'list_all'), 
    url(r'^(?P<pk>\d+)/detail/$', ReporterDetailView.as_view(template_name='reporters/detail.html')),
    url(r'^(?P<reporter_id>\d+)/edit/$', 'edit'),

)

index.html

{% load staticfiles %}
{% load render_table from django_tables2 %}
<link rel="stylesheet" type="text/css" href="{% static 'reporters/style.css' %}" />
{% if table %}
<div>
    {%render_table table %}
</div>
{% else %}
    <p>No Reporters are available.</p>
{% endif %}

非常感谢!

这是完整的异常标头。

NoReverseMatch at /reporters/
Reverse for 'mysite.apps.reporters.views.detail' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []

整个例外情况都在此粘贴框here中。

2 个答案:

答案 0 :(得分:2)

使用直接导入的基于类的视图,您需要为视图指定一个名称,以便从其他地方引用它:

urls.py

urlpatterns = patterns('mysite.apps.reporters.views',
    url(r'^$', 'index'), 
    url(r'^list_all/$', 'list_all'), 
    url(r'^(?P<pk>\d+)/detail/$', ReporterDetailView.as_view(template_name='reporters/detail.html'), name='reporter_detail'),
    url(r'^(?P<reporter_id>\d+)/edit/$', 'edit'),

然后在ReporterTable

id = tables.LinkColumn('reporter_detail', args=[A('pk')])

答案 1 :(得分:0)

args应该是一个元组,所以这样的东西而不是列表:

id = tables.LinkColumn('mysite.apps.reporters.views.detail', args=(A('pk'),))