我有点困惑,并希望利用DetailView功能使用外键作为我的过滤器来显示数据。基本上我的模式是这样的:Django DetailView按国外键筛选
class Category(models.Model):
name = models.CharField(max_length=30)
slug = models.SlugField(help_text="A short name for Category")
def __unicode__(self):
return self.name
class Meta:
ordering = ["-name"]
verbose_name_plural = "categories"
class Publisher(models.Model):
name = models.CharField(max_length=30)
slug = models.SlugField(help_text="A short name for Publisher")
class Meta:
ordering = ["-name"]
def __unicode__(self):
return self.name
class Book(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField(help_text="A short name for book")
pub_date = models.DateField()
publisher = models.ForeignKey(Publisher)
category = models.ForeignKey(Category)
class Meta:
ordering = ["-title"]
def __unicode__(self):
return self.title
我Urls.py:
url(r'^categories/(?P<slug>[\w-]+)/$', DetailView.as_view(model=Category,template_name="books/category_detail")),
我Category_detail.html
{% block content %}
<h2>{{ category.name}} </h2>
<ul>
<li>Book Title: {{ book.title }}</li>
<li>Book publisher: {{ book.publisher }}</li>
<li>Book Published Date: {{ book.pub_date }}</li>
</ul>
{% endblock %}
基本上我想在我的category_detail.html显示以下信息:
任何帮助,将不胜感激。
谢谢你 - Keoko