我有点困惑,想利用 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