所有类别的所有类别的django类视图 [英] django class based views for all categories with all entires
问题描述
我已经尝试了几个小时,看了很多文档,但是我无法理解。我不认为我会尽快看到一个解决方案,所以也许有人可以看到有什么问题?
我希望看到我的所有类别和所有我的连接的条目到这些类别。
我试图按照这个例子: django class-based-views主题
但是我得到这个错误:元组索引超出范围
我的模型:
STATUS_CHOICES =(
('d','Draft'),
('p','发布'),
('w','Whitdrawn'),
)
class PublishedManager(models.Manager):
use_for_related_fields = True
def get_query_set(self):
return super(PublishedManager,self).get_query_set() (status ='p')
class类别(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'选择= STATUS_CHOICES)
published = PublishedManager()
我的网址
#查看所有类别和所有连接的条目
url(r'^ blog / $',AllCategories.as_view()),
#查看一个类别 - 所有连接的条目
url(r'^ blog /(?P< slug> [ - \w] +)/ $',CategoryList.as_view()),
我的意见
class AllCategories(ListView):
/ pre>
context_object_name =category_list
queryset = Category.published.all()
class CategoryList(ListView):
template_name ='blog / categ ory_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):
#首先调用基本实现来获取上下文
context = super(CategoryList, self).get_context_data(** kwargs)
#添加类别
上下文['category'] = self.category
返回上下文
任何帮助非常感谢!
编辑:
添加了我的自定义管理器。
我只有在我的模板中没有显示未发布的内容的问题,列出全部我的类别和所有他们的连接方式,如:
- 类别1
- 发布的条目1
- 发布的条目3
- 类别2
- 已发布条目7
- 发布条目9
-
我用这个循环获取连接的,但它也列出未发布的内容:
{%for category.entry_set.all%}
您可以使用_set.all,我的代码:
views.py
类别(models.Model):
model =类别
template_name =#...
paginate_by =#...
模板
类别列表:
{%object_list%中的类别}
名称:{{category}}
{%for category.entry_set.all |切片::2%}
{{entry}}
{%empty%}
无
{%endfor%}
{%endfor%}
I have tried this for a couple of hours, and looked at alot of documentation, but I cant get it right. I dont think I'll see a solution to this anytime soon, so maybe someone could see what is wrong?
I want a view to show all my categories and all my connected entries to those categories.
I have tried to follow this example: django class-based-views topic
But I get this error: tuple index out of range
My model:
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()
My urls
#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()),
My views
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
Any help is much appreciated!
Edit:
Added my custom manager. I only have problems not showing unpublised entires in my template for listing all my categories and all their connected entires like:
- Category 1
- Published entry 1
- Published entry 3
- Category 2
- Published entry 7
- Published entry 9
I use this for loop getting the connected entires, but it also list the unpublised entires:
{% for entry in category.entry_set.all %}
You can use _set.all, my code:
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 %}
这篇关于所有类别的所有类别的django类视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!