def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context.update({ 'sidebars': Sidebar.get_all(), }) context.update(Category.get_navs()) return context
def post_list(request, category_id=None, tag_id=None): """ :param request: :param category_id: :param tag_id: content = 'post_list category_id={category_id}, tag_id={tag_id}'.format( category_id=category_id, tag_id=tag_id, ) # 括号内直接跟一个具体的字符串作为响应体 return HttpResponse(content) """ # def render(request, template_name, context=None, content_type=None, status=None, using=None): # template_name:模板名称,页面代码路径,contex:字典数据传输, content_type,页面编码,默认text/html, tag = None category = None if tag_id: post_list, tag = Post.get_by_tag(tag_id) elif category_id: post_list, category = Post.get_by_category(category_id) else: post_list = Post.latest_post() context = { 'category': category, 'tag': tag, 'post_list': post_list, 'sidebars': SiderBar.get_all(), } context.update(Category.get_navs()) return render(request, 'blog/list.html', context=context)
def get_content_data(self, **kwargs): context = super(CommonViewMixin, self).get_content_data(**kwargs) context.update({ 'sidebars': SideBar.get_all(), }) context.update(Category.get_navs()) return context
def get_context_data(self, **kwargs): """获取上下文数据并最终将其传入模板""" context = super().get_context_data(**kwargs) context.update({'sidebars': SiderBar.get_all()}) context.update(Category.get_navs()) # print('======CommonViewMixin.get_context_date():', context, '======') return context
def detall(request, post_id=None): try: post = Post.objects.get(id=post_id) except Exception as e: post = None context = {'post': post} context.update(Category.get_navs(Category)) return render(request, 'detall.html', context=context)
def get_context_data(self, **kwargs): from config.models import SideBar context = super().get_context_data(**kwargs) context.update({ 'sidebars': SideBar.get_all(), }) context.update(Category.get_navs()) return context
def post_detail(request, post_id): post = Post.objects.filter(id=post_id).first() handle_visited(request, post_id) context = { 'post': post, 'sidebars': SideBar.get_all(), } context.update(Category.get_navs()) return render(request, 'blog/detail.html', context=context)
def post_detail(request, post_id=None): try: post = Post.objects.get(id=post_id) except Post.DoesNotExist: post = None context = {'post': post, 'sidebars': SideBar.get_all()} context.update(Category.get_navs()) return render(request, 'blog/detail.html', context=context)
def post_detail(request, post_id=None): # return HttpResponse('detail') # return render(request, 'blog/detail.html', context={'name': 'post_detail'}) try: post = Post.objects.get(id=post_id) except Post.DoesNotExist: post = None context = { 'post': post, 'sidebars': SideBar.get_all(), } context.update(Category.get_navs()) return render(request, 'blog/detail.html', context={'post': post})
def post_list(request, category_id=None, tag_id=None): tag = None category = None ''' if tag_id: try : tag = Tag.objects.get(id=tag_id) except Tag.DoesNotExist: post_list = [] else: post_list = tag.post_set.filter(status=Post.STATUS_NORMAL) else: post_list = Post.objects.filter(status=Post.STATUS_NORMAL) if category_id: try: category = Category.objects.get(id=category_id) except Category.DoesNotExist: category = None else: post_list = post_list.Filter(category_id=category_id) context = { 'category':category, 'tag':tag, 'post_list':post_list, } return render(request, 'blog/list.html', context=context) ''' ''' 如果查询到不存在的对象,需要通过try...except...来捕获并处理异常,避免当数据不存在时出现错误 tag与post是多对多关系,因此需要先获取tag对象,接着通过该对象来获取对应的文章列表。 ''' if tag_id: post_list, tag = Post.get_by_tag(tag_id) elif category_id: post_list, category = Post.get_by_category(category_id) else: post_list = Post.latest_posts() from config.models import SideBar context = { 'category':category, 'tag': tag, 'post_list': post_list, 'sidebars': SideBar.get_all(), } context.update(Category.get_navs()) return render(request, 'blog/list.html', context=context)
def get_context_data(self, **kwargs): context = super(PostListView, self).get_context_data(**kwargs) context.update({ 'category': self.category, 'tag': self.tag, 'owner': self.owner, 'sidebars': SideBar.get_all(), }) context.update(Category.get_navs()) if self.request.GET.get('search'): context.update({ 'keyword': self.request.GET.get('search'), }) return context
def get_context_data(self, **kwargs): if isinstance(self.request.user, AnonymousUser): sidebars = None user = '******' else: sidebars = SideBar.objects.filter(owner=self.request.user) user = self.request.user context = super(CommonViewMixin, self).get_context_data(**kwargs) context.update({ 'sidebars': sidebars, 'user': user }) if self.request.method == 'GET': context.update(Category.get_navs(owner=self.request.user.id)) return context
def post_list(request, category_id=None, tag_id=None): tag = None category = None if tag_id: post_list, tag = Post.get_by_tag(tag_id) elif category_id: post_list, category = Post.get_by_category(category_id) else: post_list = Post.latest_posts() context = { 'category': category, 'tag': tag, 'post_list': post_list, 'sidebars': SideBar.get_all(), } context.update(Category.get_navs()) return render(request, 'blog/list.html', context=context)
def post_list(request, category_id=None, tag_id=None): tag = None category = None if tag_id: post_list, tag = Post.get_by_tag(tag_id) elif category_id: post_list, category = Post.get_by_category(category_id) else: post_list = Post.latest_postlist(Post) context = { 'category': category, 'tag': tag, 'post_list': post_list, 'sidebars': SideBar.get_all(SideBar), } context.update(Category.get_navs(Category)) # N + 1 测试demo # post_list = Post.objects.all().select_related('owner','category') # for post in post_list: # print(post.id) return render(request, 'list.html', context=context)
def post_list(request, category_id=None, tag_id=None): # content = 'post_list category_id={category_id}, tag_id={tag_id}'.format(category_id=category_id, tag_id=tag_id) # return HttpResponse(content) # return render(request, 'blog/list.html', context={'name': 'post_list'}) tag = None category = None if tag_id: post_list, tag = Post.get_by_tag(tag_id) elif category_id: post_list, category = Post.get_by_category(category_id) else: post_list = Post.latest_posts() # if tag_id: # try: # tag = Tag.objects.get(id=tag_id) # except Tag.DoesNotExist: # post_list = [] # else: # post_list = tag.post_set.filter(status=Post.STATUS_NORMAL) # else: # post_list = Post.objects.filter(status=Post.STATUS_NORMAL) # if category_id: # try: # category = Category.objects.get(id=category_id) # except Category.DoesNotExist: # category = None # else: # post_list = post_list.filter(category_id=category_id) context = { 'category': category, 'tag': tag, 'post_list': post_list, 'sidebars': SideBar.get_all(), } context.update(Category.get_navs()) return render(request, 'blog/list.html', context=context)
def get_context(self): ctx = super(ElasSearchView, self).get_context() tmp = {'sidebars': SideBar.get_all()} ctx.update(tmp) ctx.update(Category.get_navs()) return ctx