我正在尝试使用 Django 1.6 构建我自己的博客应用程序。我已经通过这样的通用视图生成了一个类别列表:
网址.py
url(r'^categories/?$', views.ListView.as_view(model=Category), name='categories'),
category_list.html
<h3>Categories</h3>
{% for category in object_list %}
<ul>
<li>{{ category.title }}</li>
</ul>
{% endfor %}
所有类别现在都列在/categories
。
我的问题是当我将它添加到base.html
或index.html
文件时,输出更改为article.title
notcategory.title
如何将此类别列表添加到其他页面,例如索引或文章?这是我完整的 views.py 文件:
视图.py
from django.shortcuts import get_object_or_404, render
from django.views.generic import ListView, DetailView
from blog.models import Article, Category
class IndexView(ListView):
template_name = 'blog/index.html'
context_object_name = 'latest_article_list'
def get_queryset(self):
return Article.objects.order_by('-pub_date')[:10]
class ArticleView(DetailView):
model = Article
template_name = 'blog/article.html'