我正在使用 Django 1.4.5。
当我通过管理员页面访问条目详细信息时出现错误。
“NoReverseMatch at /admin/r/12/1/”:使用参数 '()' 和关键字参数 '{'category': u'category', 'slug': u'entry-test'} ' 未找到
访问首页(/category/entry-test/)上的条目详细信息时,一无所获/空白页。
如果我想通过子类别(/category/subcategory/entry-test/)访问条目详细信息,模型和 URLname 模式的权利是什么?
模型片段:
class Entry (models.Model):
title = models.CharField()
slug = models.SlugField()
category = models.ForeignKey('entry.Category')
def __unicode__(self):
return self.title
@models.permalink
def get_absolute_url(self):
return ('entry.views.detail', (), {'category': self.category.slug, 'slug': self.slug})
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
return super(Entry, self).save(*args, **kwargs)
class Category(models.Model):
title = models.CharField()
slug = models.SlugField()
parent = models.ForeignKey('self', blank=True, null=True, related_name='children')
def __unicode__(self):
if self.parent:
return self.parent.title, self.title
return self.title
@permalink
def get_absolute_url(self):
return ('entry.views.category', (), {'slug': self.slug})
浏览片段:
def category(request):
category = Category.objects.all()
return render_to_response('category.html', locals(), context_instance=RequestContext(request))
def list(request, slug_id):
category = get_object_or_404(Category, slug=slug_id)
list = Entry.objects.filter(category=category)
return render_to_response("list.html", locals(), context_instance=RequestContext(request))
def detail(request, category_id, slug_id):
entry = Entry.objects.filter(slug=slug_id)
category = Category.objects.filter(slug=category_id)
return render_to_response('detail.html', locals(), context_instance=RequestContext(request))
网址片段:
urlpatterns = patterns('entry.views',
(r'^$', 'category', name='entry-category'),
url(r'^(?P<slug_id>[-\w]+)/$', 'list', name='entry-list'),
url(r'^(?P<category_id>[-\w]+)/(?P<slug_id>[-\w]+)/$', 'detail', name='entry-detail'),
模板片段:
类别.html
<ul>
{% for category in category %}
<li><a href="{{ category.slug }}">{{ category.title }}</a> ({{ category.entry_set.all.count }})</li>
{% endfor %}
</ul>
列表.html
<ul>
{% for entry in list %}
<li><a href="{{ entry.slug }}">{{ entry.title }}</a></li>
{% endfor %}
</ul>
详细信息.html
<ul>
{% for entry in entry.category %}
<li><a href="{{ entry.category }}/{{ entry.get_absolute_url }}">{{ entry.title }}</a>
<br />{{ entry.description }}</li>
{% endfor %}
</ul>
我在哪里做错了什么?如果有人可以提供帮助,我将不胜感激!请帮我。