views.py
class PostDetail(DetailView):
template_name = "posts/single.html"
model = Post
context_object_name = "single"
def get_context_data(self, **kwargs):
context = super(PostDetail, self).get_context_data(**kwargs)
context['allCategories'] = Category.objects.all()
return context
models.py
class PostDetail(DetailView):
template_name = "posts/single.html"
model = Post
context_object_name = "single"
def get_context_data(self, **kwargs):
context = super(PostDetail, self).get_context_data(**kwargs)
context['allCategories'] = Category.objects.all()
return context
single.html
<ul>
{% for category in allCategories %}
<li>
<a href="#">{{ category.title }} <span>{{ category.postCount }}</span></a>
</li>
{% endfor %}
</ul>
</div>
我在博客中看不到类别。而且我没有任何警告。