Just started with Django but hit a bit of a wall - I decided to experiment with writing a simple blog engine while referring to the django-basic-apps library.
In blog/urls.py, I have this entry to map to the actual post by date, e.g. 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 post:
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 implemented get_absolute_url
so that one the main blog page I could click on a post's title to view it:
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, in the main page's post list, the permalink is supposed to be inserted in the title:
{% 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 the django shell the error gets thrown:
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 it was a Python namespace problem (see below). But anyway, was my urls.py as shown above incorrect?