2014-03-26 50 views
0

從記者的主視圖鏈接到我的DetailView時遇到問題。兩個網址都可以。我知道這個問題是有據可查的,但URLs非常混亂,我似乎無法找到解決方案。 這裏是我的views.py,table.py(錯誤是由於在這一行),urls.py的代碼。Django無法鏈接到DetailView的NoReverseMatch錯誤

幫助將不勝感激。 鏈接是/記者/這應該鏈接到/記者/ PK /詳細 `

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

+1

你能發佈例外的全文嗎? – ACGray

+0

@ACGray我在底部添加了鏈接到整個異常的文本 – Timmay

回答

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')]) 
+0

非常感謝。它解決了這個問題。現在讓我繼續更多的東西。 – Timmay

0

args應該是一個元組,這樣類似的東西,而不是一個列表:

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

我不認爲這是問題所在。這並沒有解決它。 – Timmay