return 42;

by Jan Wedel

NoReverseMatch at /admin/r/14/13/

This article describes how I resolve an error Django gave me

I was just recently about to wrote a preview pane for my Django blog admin page, especially for the article admin page. Then I saw the "View on site" button on the top right. When I clicked it, I got this:

NoReverseMatch at /admin/r/14/13/
Reverse for 'blog.views.post' with arguments '(u'my-articles-slug',)' and keyword arguments '{}' not found. 0 pattern(s) tried: []

So why didn't it work? It was my first Django project and it was quite some time ago since I wrote it, so I wasn't sure what this was about. Clearly, the stack trace told me when the problem was caused:

/home/jaw/devel/python/django-return-code/blog/models.py in get_absolute_url
    return reverse('blog.views.post', args=[self.slug]) ...

OK, so there must be something wrong with blog.views.post but I couldn't remember the semantics. This was clearly taken from the Django example. I could at least guess, that blog was the Django app, views linked to my views and post would then be the name of the view or more specifically, the entry in the urls:

url(r'^articles/(?P<slug>[\w_-]+)/$', views.ArticleDetailView.as_view(), name='article'),

Oops! There it is, it's article. There is no view named post. Yeah, I remember that I actually changed the name at some point. Ok, that should be easy:

return reverse('blog.views.article', args=[self.slug])

No. Unfortunately not. Still the same error. After some time, I found this which gave me a hint.

For example, the main index page of the admin application is referenced using 'admin:index'. This indicates a namespace of 'admin', and a named URL of 'index'.

So I changed it to

    def get_absolute_url(self):
        return reverse('blog:article', args=[self.slug])

and it worked! Again, Django has really an outstanding documentation!