Django newbie - NoReverseMatch errors
Just started with Django, but hit the wall a bit - I decided to experiment with writing a simple blog mechanism by accessing the django-basic-apps library.
In the blog /urls.py I have this post to map an actual post by date, eg. Blog / 2009 / Aug / 01 / test post
urlpatterns = patterns('',
url(r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/$', 'blog.views.post_detail'),
...
And the view for rendering the message:
def post_detail(request, slug, year, month, day, **kwargs):
return date_based.object_detail(
request,
year = year,
month = month,
day = day,
date_field = 'created_at',
slug = slug,
queryset = Content.objects.filter(published=True),
**kwargs
)
In the model, I have implemented so that on the blog homepage I can click on the post title to view it: get_absolute_url
class Content(models.Model):
...
@permalink
def get_absolute_url(self):
return ('blog.views.post_detail', (), {
'slug': self.slug,
'year': self.created_at.year,
'month': self.created_at.strftime('%b').lower(),
'day': self.created_at.day
})
Finally, the post list on the homepage assumes that the permalink will be inserted in the header:
{% for content in object_list %}
<div class="content_list">
<h3 class="content_title"><a href="{{ content.get_absolute_url }}">{{ content.title }}</a></h3>
<p class="content_date">{{ content.published_at|date:"Y F d"}}</p>
<p class="content_body">{{ content.body }}</p>
<p class="content_footer">updated by {{ content.author }} at {{ content.updated_at|timesince }} ago</p>
</div>
{% endfor %}
However, the link shows up as empty and when I try to call content.get_absolute_url()
from django shell the error occurs:
NoReverseMatch: Reverse for '<function post_detail at 0xa3d59cc>' with arguments '()' and keyword arguments '{'year': 2009, 'slug': u'another_test', 'day': 15, 'month': 'aug'}' not found.
Edit: Turns out this is a Python namespace issue (see below). But anyway, was my urls.py as shown above wrong?
source to share
Googling for other newbies to the Django tutorials and got the idea of ββputting all urls in the parent urls.py folder and that seems to fix the problem. :) So at the end my main urls.py now has:
from djangoblog.blog import views
urlpatterns = patterns('',
(r'^blog/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/$',
views.post_detail),
(r'^blog/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{1,2})/$',
views.post_archive_day),
...
Edit: Edit: After 2 days of random hacking, I luckily realize that URLconfs + django looks much better. :) I moved the templates back to blog / urls.py, got rid of all custom date based views and called them from urls.py instead and correctly named templates for elements to be relabeled.
urls.py with named template:
from blog import views
...
(r'(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/$',
'object_detail', dict(info_dict, slug_field='slug', month_format='%m'),
'post_detail'),
...
(r'category/(?P<slug>[-\w]+)/$', views.category_detail),
models.py:
class Post:
@permalink
def get_absolute_url(self):
return ('post_detail', (), {
....
class Category:
@permalink
def get_absolute_url(self):
return ('blog.views.category_detail', (), {'slug': self.slug})
source to share