这是我的django应用程序的一部分,我想用get_absolute_url创建链接,但是我收到错误:
Reverse for 'details' with arguments '()' and keyword arguments '{'slug': 'product'}' not found. 0 pattern(s) tried: []
我的模特:
class PortfolioItem(models.Model):
name_item = models.CharField(max_length=120)
slug = models.SlugField(unique=True)
date_from = models.DateField('date from')
date_to = models.DateField('date to')
description = models.TextField()
author = models.ForeignKey(User)
def __str__(self):
return self.name_item
def get_absolute_url(self):
return reverse('details', kwargs={"slug": self.slug})
以下是我的观点:
class PortfolioDetail(DetailView):
model = PortfolioItem
template_name = "portfoliodetail.html"
这是我的网址:
urlpatterns = [
url(r'^$', PortfolioList.as_view(), name='home'),
url(r'^portfolio/(?P<slug>\w+)/$', PortfolioDetail.as_view(), name='details'),
]
这是一个模板:
<ul>
{% for i in portfolioitem_list %}
<li><a href="{{ i.get_absolute_url }}">{{ i.name_item }}</a></li>
{% endfor %}
</ul>
答案 0 :(得分:1)
引用url时需要包含命名空间
return reverse('portfolio:details', kwargs={"slug": self.slug})