i am trying to get the absolute url ... and having an error.
我试图获得绝对url…有一个错误。
this is my models.py
这是我models.py
class News(models.Model):
date = models.DateField(auto_now=True)
title = models.CharField(max_length=100)
slug = models.CharField(max_length=100)
text = models.TextField()
def __unicode__(self):
return self.title
def get_absolute_url(self):
return ('object detail', {}, {
'slug': self.slug,
'year': self.date.strftime('%Y'),
'month': self.date.strftime('%b'),
'day': self.date.strftime('%d')
})
class Meta:
verbose_name_plural = "News"
ordering = ('-date',)
and this is my urls.py
这是我的urls.py。
dictionary = {
'queryset': News.objects.all(),
'date_field': 'date',
}
urlpatterns= patterns(
'django.views.generic.date_based',
url(
r'(?P<year>d{4})/(?P<month>[a-z]{3})/(?P<day>w{1,2})/(?P<slug>[-w]+)/$',
'object_detail',
dict(dictionary, slug_field = 'slug', template_name = 'cms/news/object.html'),
name='object detail'
),
)
when i do this is django shell
当我这样做的时候,是django shell。
>> from cms.models import News
>> all = News.objects.all()
>> for single in all:
>> single.get_absolute_url()
i get this following error in console.
我在控制台得到了以下错误。
Traceback (most recent call last):
File "<console>", line 2, in <module>
File "/Users/mouse/.virtualenvs/proj/lib/python2.7/site-packages/django/utils/functional.py", line 11, in _curried
return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs))
File "/Users/mouse/.virtualenvs/proj/lib/python2.7/site-packages/django/db/models/base.py", line 883, in get_absolute_url
return settings.ABSOLUTE_URL_OVERRIDES.get('%s.%s' % (opts.app_label, opts.module_name), func)(self, *args, **kwargs)
File "/Users/mouse/.virtualenvs/proj/lib/python2.7/site-packages/django/db/models/__init__.py", line 35, in inner
return reverse(bits[0], None, *bits[1:3])
File "/Users/mouse/.virtualenvs/proj/lib/python2.7/site-packages/django/core/urlresolvers.py", line 476, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/Users/mouse/.virtualenvs/proj/lib/python2.7/site-packages/django/core/urlresolvers.py", line 396, in _reverse_with_prefix
"arguments '%s' not found." % (lookup_view_s, args, kwargs))
NoReverseMatch: Reverse for 'object detail' with arguments '()' and keyword arguments '{'year': '2013', 'slug': u'2013-season-details', 'day': '19', 'month': 'Feb'}' not found.
where and what i could be doing wrong?
我可能做错了什么?
1 个解决方案
#1
2
You need to satisfy the year
, month
and day
keyword arguments in your get_absolute_url method in addition to the slug
. I would also pass in an empty tuple instead of None
for the positional arguments.
除了slug之外,您还需要在get_absolute_url方法中满足年份、月和日关键字参数。我也会传递一个空的元组,而不是所有的位置参数。
#1
2
You need to satisfy the year
, month
and day
keyword arguments in your get_absolute_url method in addition to the slug
. I would also pass in an empty tuple instead of None
for the positional arguments.
除了slug之外,您还需要在get_absolute_url方法中满足年份、月和日关键字参数。我也会传递一个空的元组,而不是所有的位置参数。