I am little puzzled and would like to utilized the DetailView functionality using the foreign key as my filter to display the data. Basically my model looks like this:
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
My Urls.py:
url(r'^categories/(?P<slug>[\w-]+)/$', DetailView.as_view(model=Category,template_name="books/category_detail" )),
My 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 %}
Basically I would like to display in my category_detail.html the following information:
Any help would be appreciated.
Thank you - Keoko
In template you have category object. you can iterate all over the books;
or just first book;
Thank you for your kind response. I created a views.py file with the following information:
And updated my application urls.py:
And finally modified the category_detail.html with the following: