0
votes

I'm having trouble with viewing single posts for my blog. I'm trying to implement urls that use a slugline and content id. Here's my error:

NoReverseMatch at /admin/r/7/2/ Reverse for 'article' with arguments '()' and keyword arguments '{'slugline': u'test-post-3', 'id': 2}' not found. 0 pattern(s) tried: []

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?'
1
Why is it showing "admin"?...are you looking in the correct url? - cdvv7788
You don't appear to have an /admin/ url pattern configured, but the error message does seem to indicate you are trying to access the admin panel. - Nostalg.io
I was wondering that, too. The admin page is mapped in the main url.py. I don't know why it's bringing me there while trying to access the 'detail' page - Cameron Sima

1 Answers

0
votes

I think get_absolute_url should be :

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