2014-10-18 35 views
0

我在查看博客文章時遇到問題。我試圖實現使用slugline和內容id的url。這裏是我的錯誤:Django NoReverseMatch在/ admin/r/7/2 /錯誤地址

NoReverseMatch at/admin/r/7/2/ 反轉'文章'與參數'()'和關鍵字參數'{'slugline':u'test-post-3',' id':2}'找不到。 0圖案(多個)嘗試:[]

views.py:

from django.shortcuts import render, get_object_or_404 
from django.http import HttpResponse, HttpResponseRedirect 
from django.template import RequestContext, loader 
from django.core.urlresolvers import reverse 
from django.views import generic 
from django.utils import timezone 

# Create your views here. 

from articles.models import Content 

class IndexView(generic.ListView): 
    template_name = 'articles/index.html' 
    context_object_name = 'latest_articles_list' 

    def get_queryset(self): 
     return Content.objects.filter(
      published_date__lte=timezone.now() 
     ).order_by('-published_date')[:5] 


def detail(request, slugline, poll_id): 
    article = get_object_or_404(pk=poll_id) 
    return render(request, 'articles/detail.html', {'article': article}) 

urls.py:

from django.conf.urls import patterns, url 
from articles import views 

urlpatterns = patterns('', 
    url(r'^$', views.IndexView.as_view(), name = 'index'), 
    #url(r'^(?P<slugline>[-\w\d]+), (?P<pk>\d+)/$', views.DetailView.as_view(), name='detail') 
    url(r'^(?P<slugline>[-\w\d]+),(?P<id>\d+)/$', view=views.detail, name='article'), 
) 

models.py:

from django.db import models 
from django.db.models import permalink 
from django.utils import timezone 
import datetime 
# Create your models here. 


class Content(models.Model): 
    title = models.CharField(max_length=100, unique=True) 
    slugline = models.SlugField(max_length=100, unique=True) 
    body = models.TextField() 
    published_date = models.DateTimeField('date published') 

    def __unicode__(self): 
     return self.title 

    @permalink 
    def get_absolute_url(self): 
     return ('article',(), { 
      'slugline': self.slugline, 
      'id': self.id, 
     }) 

    def was_published_recently(self): 
     now = timezone.now() 
     return now - datetime.timedelta(days=1) <= self.published_date <= now 
    was_published_recently.admin_order_field = 'published_date' 
    was_published_recently.boolean = True 
    was_published_recently.short_description = 'Published recently?' 
+0

它爲什麼顯示「admin」?...你在尋找正確的網址嗎? – cdvv7788 2014-10-18 22:40:59

+0

您似乎未配置/ admin/url模式,但錯誤消息確實表明您正在嘗試訪問管理面板。 – 2014-10-18 22:53:23

+0

我也想知道。管理頁面被映射到主要的url.py中。我不知道爲什麼它在嘗試訪問「詳細信息」頁面時將我帶到那裏 – 2014-10-20 20:51:19

回答

0

我認爲get_absolute_url應該be:

@permalink 
def get_absolute_url(self): 
    from django.core.urlresolvers import reverse 
    return reverse('article', args=[self.slugline,str(self.id)])