views:

288

answers:

1

Is there an easy way to get the URL to a Django date-based generic view (specifically object_detail) from a template?

Given a URL conf like this

url(r'^posts/(?P<year>\d\d\d\d)/(?P<month>\d\d)/(?P<day>\d\d)/(?P<slug>[\w-]+)$', 'date_based.object_detail', detail_info, name='blog-post-detail')

My understanding is that I would need to do something like this

{% url blog_post_detail year=post.date.year.str,month=post.date.month,day=post.date.day,slug=post.slug %}

Except that

  1. The year, month and day need to be formatted as strings
  2. There has to be an easier way

If anyone has input on either of these it would be much appreciated

Thanks

+2  A: 

The easier and better way is to define this reverse in the BlogPost model within get_absolute_url and in the template use {{blogpost.get_absolute_url}}

Update:

Assuming you are storing the date in the models.DateField or models.DateTimeField, the string conversion can be achieved by date.strftime("%Y/%b/%d").lower()

Lakshman Prasad
Thats exactly what I needed, thanks!
russell_h