我计划用Django写博客。在我的博客页面中,我想展示博客和评论,我试图使用DetailView
class BlogDetailView(DetailView):
template_name = 'inventory/item_detail.html'
model = Blogitem
def get_context_data(self, **kwargs):
context = super(BlogDetailView, self).get_context_data(**kwargs)
pk = self.kwargs.get('id')
try:
item = Blogitem.objects.get(pk=pk)
item.view_number = item.view_number + 1
item.save()
comment_list = Comment.objects.filter(comment_blog_id=pk).order_by(F('comment_date').asc())
context['item'] = item
context['comment_list'] = comment_list
except Blogitem.DoesNotExist:
raise Http404
return context
错误:通用详细信息视图必须使用对象pk或slug调用BlogDetailView。
我知道我需要添加model = somethings
等代码或使用get_queryset(self):
下面的template_name = 'inventory/item_detail.html'
获取查询集
如果我在template_name = 'inventory/item_detail.html'
model = Blogitem, Comment
我知道 错误:'tuple'对象没有属性'_default_manager'
urls.py
url(r'^blog/(?P<id>(\d+))/$',views.BlogDetailView.as_view() , name='item_detail')
models.py
class Blogitem(models.Model):
title = models.CharField(max_length=200)
content_html = models.TextField() #html
content_markdown = models.TextField() #markdown
publication_date = models.DateTimeField()
update_date = models.DateTimeField()
first_tag = models.CharField(max_length=200,
default= 'other')
tag_supplement = models.CharField(max_length=100) #tag supplement
view_number = models.IntegerField()
def __str__(self):
return (u'%s %s') %(self.title,self.publication_date)
class Admin:
pass
class Meta:
ordering = ['publication_date']
verbose_name_plural = verbose_name = 'blog'
class Comment(models.Model):
comment_blog = models.ForeignKey(Blogitem, verbose_name=u'blog')
comment_user = models.ForeignKey(UserProfile, verbose_name= u'user',blank= True,null = True)
comment_date = models.DateTimeField(verbose_name=u'comment date',auto_now = True)
comment_content = models.TextField(verbose_name=u'comment words')
comment_approved = models.BooleanField(verbose_name=u'comment approved',default= True)
comment_like = models.IntegerField(verbose_name=u'like number',default=0)
def __str__(self):
return (u'%s %s %s %s') % (self.comment_blog, self.comment_user, self.comment_content, self.comment_date)
class Meta:
ordering = ['-comment_date']
verbose_name_plural = verbose_name = 'comment'
我知道我可以使用基于功能的视图修复错误,但不能修复基于类的视图:
urls.py
url(r'^blog/(?P<id>(\d+))/$',views.item_detail , name='item_detail')
views.py
def item_detail(request, id):
'''
detail of blog
'''
try:
item = Blogitem.objects.get(id=id)
item.view_number = item.view_number + 1
item.save()
comment_list = Comment.objects.filter(comment_blog_id=item.id).order_by(F('comment_date').asc())
except Blogitem.DoesNotExist:
raise Http404
return render_to_response( 'inventory/item_detail.html', {'item': item,'comment_list': comment_list}, context_instance=RequestContext(request))
DetailView如何适用于两个模型,或者如何使用基于类的视图来处理两个模型?
答案 0 :(得分:1)
您必须在详情视图中指定model
。并在get_context_data
class BlogDetailView(DetailView):
model = Blogitem
template_name = 'inventory/item_detail.html'
slug_field = 'pk'
def get_context_data(self, **kwargs):
context = super(BlogDetailView, self).get_context_data(**kwargs)
context['comment_list'] = Comment.objects.filter(comment_blog_id=pk).order_by(F('comment_date').asc())
# other code
return context
urls.py
url(r'^blog/(?P<pk>(\d+))/$',views.BlogDetailView.as_view() , name='item_detail')