0

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'),
    ...
)
4

1 回答 1

1

没有urls.py我只能猜测,但很可能它需要两位数的月份和日期标识符,而在某些情况下您只提供一位数。无论如何-这是您的旧代码和新代码之间的区别get_absolute_url-在旧代码中的月份和日期数字中,如有必要,用 0 填充。

尝试这个:

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
        ])
于 2013-10-13T19:44:51.580 回答