Reputation: 77
I was updating the get_absolute_url definitions in an app and it stopped working for some reason. Haven't been able to figure out exactly why. Even stranger is that everything works fine on my local machine but doesn't work on the server (Webfaction).
Original code, which worked, was:
def get_absolute_url(self):
return "/blog/%s/%02d/%02d/%s/" % (self.publication_date.year, self.publication_date.month, self.publication_date.day, self.URL)
I changed that to:
def get_absolute_url(self):
return reverse('blog-post', args=[
self.publication_date.year,
self.publication_date.month,
self.publication_date.day,
self.URL
])
It failed silently. When I clicked the "View on site" link in the admin, I got this error:
Reverse for 'blog-post' with arguments '(2013, 7, 19, 'some-test-slug')' and keyword arguments '{}' not found.
I tried using keyword arguments instead:
def get_absolute_url(self):
return reverse('blog-post', kwargs={
'year': self.publication_date.year,
...
})
...but that didn't have any affect.
Remember, the strange thing is that everything is fine in my local environment.
Using Django 1.3.7 for this, FWIW.
Thanks for your help.
Here's the urls.py:
urlpatterns = patterns('blog.views',
...
url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<slug>[a-zA-Z0-9_.-]+)/$', 'post', name='blog-post'),
...
)
Upvotes: 0
Views: 90
Reputation: 25062
Without urls.py
I can only guess, but most likely it requires two digit month and day identifiers, while in some cases you provide only one digit. Anyway - that is the difference between your old and your new get_absolute_url
code - in the old code month and day numbers where padded by 0 if necessary.
Try this:
def get_absolute_url(self):
return reverse('blog-post', args=[
self.publication_date.year,
self.publication_date.strftime('%m'),
self.publication_date.strftime('%d'),
self.URL
])
Upvotes: 1