django:DetailView 如何为两个模型工作或基于类的视图如何为两个模型工作?
Posted
技术标签:
【中文标题】django:DetailView 如何为两个模型工作或基于类的视图如何为两个模型工作?【英文标题】:django: How DetailView work for two model or how class-based-views work for two model? 【发布时间】:2016-10-26 21:47:33 【问题描述】:我打算用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
之类的代码,或者通过在template_name = 'inventory/item_detail.html'
下方使用get_queryset(self):
来获取查询集
如果我在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 如何为两个模型工作,或者我如何使用基于类的视图为两个模型工作?
【问题讨论】:
model = BlogItem
。您必须放置要显示详细信息的模型。
【参考方案1】:
您必须在详细视图中指定您的model
。并在get_context_data
中添加cmets_list
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')
【讨论】:
它不能工作,错误:/blog/8/处的AttributeError,必须使用对象pk或slug调用通用详细视图BlogDetailView。异常位置: /myvenv/lib/python3.4/site-packages/django/views/generic/detail.py 在 get_object,第 49 行 @TaylorLively 尝试删除id
并在网址中添加 pk
(?Pfilter(id=self.slug_field)
?你没有在课堂上的任何地方定义pk
。以上是关于django:DetailView 如何为两个模型工作或基于类的视图如何为两个模型工作?的主要内容,如果未能解决你的问题,请参考以下文章