【问题标题】:django class based views for all categories with all entires所有条目的所有类别的基于 django 类的视图
【发布时间】:2012-09-20 19:34:54
【问题描述】:

我已经尝试了几个小时,并查看了很多文档,但我无法正确理解。我认为我不会很快看到解决方案,所以也许有人可以看到问题所在?

我想要一个视图来显示我的所有类别以及与这些类别相关的所有条目。

我尝试过这个例子:django class-based-views topic

但我得到这个错误:元组索引超出范围

我的模特:

STATUS_CHOICES = (
    ('d', 'Draft'),
    ('p', 'Published'),
    ('w', 'Whitdrawn'),
)

class PublishedManager(models.Manager):
    use_for_related_fields = True
    def get_query_set(self):
        return super(PublishedManager, self).get_query_set().filter(status='p')

class Category(models.Model):
    name = models.CharField()
    slug = models.SlugField()
    status = models.CharField(max_length=1, default='d', choices=STATUS_CHOICES)
    published = PublishedManager()

class Entry(models.Model):
    name = models.CharField()
    slug = models.SlugField()
    category = models.ForeignKey(Category)
    status = models.CharField(max_length=1, default='d', choices=STATUS_CHOICES)
    published = PublishedManager()

我的网址

#View for all categories and all connected entries
url(r'^blog/$', AllCategories.as_view()),
#View for one category - and all the connected entries
url(r'^blog/(?P<slug>[-\w]+)/$', CategoryList.as_view()),

我的看法

class AllCategories(ListView):
    context_object_name = "category_list"
    queryset = Category.published.all()

class CategoryList(ListView):

    template_name = 'blog/category_and_connected_entries.html'

    def get_queryset(self):
        self.category = get_object_or_404(Category, self=self.kwargs['slug'])
        return Entry.published.filter(category=self.category)

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(CategoryList, self).get_context_data(**kwargs)
        # Add in the category
        context['category'] = self.category
        return context

非常感谢任何帮助!

编辑:

添加了我的自定义管理器。 我只是在我的模板中没有显示未发布的整体以列出我的 all 我的类别和 all 他们连接的整体,例如:

  • 类别 1
    • 已发布条目 1
    • 已发布条目 3
  • 类别 2
    • 已发布条目 7
    • 已发布条目 9

我使用这个 for 循环来获取连接的整体,但它也列出了未发布的整体:

{% for entry in category.entry_set.all %}

【问题讨论】:

  • 对不起,我现在去睡觉了,伙计,但我只是注意到您确实让 listview 进行了正确的查询,所以问题很可能是您的模板代码可以轻松修复。我明天检查你或其他人可以提供帮助。
  • 现在我在我的模板中执行 {% if entry.status == "p" %} ,但我认为最好获得所有已发布条目的正确查询集,我只是不这样做知道我将如何得到它。

标签: django django-class-based-views


【解决方案1】:

你得到这个错误是因为 self.args 是所有位置参数的元组是空的,因为你没有在你的 url 中指定一个组并且你试图获取一个空元组的第一个元素(self.args[0] )

将您的网址更改为:
url(r'^blog/(\w+)/$', CategoryList.as_view()), 为了能够使用self.args[0]捕获类别

复制自docs

完成这项工作的关键部分是,当 调用基于类的视图,存储各种有用的东西 自己;以及请求(self.request),这包括 捕获的位置(self.args)和基于名称(self.kwargs)的参数 根据 URLconf。

除此之外,我在您的代码中看不到任何其他错误。

【讨论】:

  • 是的,解决了它。现在我的 /blog/category_slug/ 页面可以工作了。我得到一个包含所有连接条目的列表。但是,如果我想列出我的所有类别,我该怎么办?在同一页面上,列出与每个类别相关的所有条目?喜欢
    • 类别 1
    • 条目 1
    • 条目 4
  • 类别 2
  • 条目 8
【解决方案2】:

根据您的评论,如果您只想显示具有相关条目的所有类别,那么您需要做的就是获取所有类别

def query_set(self):
    return Category.objects.all()

然后在您的模板中循环遍历类别和条目的嵌套循环

{% for category in object_list %}
    <ul>
        <li>{{ category.name }}</li>
        {% for entry in category.entry_set.all %}
            <li>{{ entry }}</li>
        {% endfor %}
    </ul>
{% endfor %}

【讨论】:

  • 哇,我一直在想办法复杂!谢谢!
  • 现在,一个新问题。我该如何使用我的自定义管理器?我有一个名为“PublishedManager”的经理,我的模型上有“published = PublishedManager()”。如何在我的模板中使用这个管理器?
  • Category.published.all()....查看 django 书籍的模型和高级模型部分了解更多信息
  • 是的,我认为我有 queryset = Package.published.all(),但是当我这样做时:{% for entry in category.entry_set.all %} 在我的模板中,我仍然得到一个未发布条目列表(我在条目模型和类别模型上使用相同的管理器)
  • 为您的模型经理发布代码......如果它很长,那么只需编辑您的问题(如果可能)
【解决方案3】:

你可以使用 _set.all,我的代码:

views.py
class Categories(models.Model):
    model = Category
    template_name = "#..."
    paginate_by = #...

template
Categories list:
{% for category in object_list %}
    Name: {{ category }}
    {% for entry in category.entry_set.all|slice:":2" %}
        {{ entry }}
    {% empty %}
       None
    {% endfor %}
{% endfor %}

【讨论】:

  • 是的。结束了这样的事情。
猜你喜欢
相关资源
最近更新 更多
热门标签