We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
说明: config/blocks/sidebar_posts.html和config/blocks/sidebar_comments.html模板文件跟Model层封装的一样。
修改blog/views.py为:
from django.http import HttpResponse from django.shortcuts import render from django.template.loader import render_to_string from config.models import SideBar from .models import Post, Tag, Category # Create your views here. def post_list(request, category_id=None, tag_id=None): category = None tag = 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() sidebars = [] for sidebar in SideBar.get_all(): if sidebar.display_type == SideBar.DISPLAY_HTML: pass elif sidebar.display_type == SideBar.DISPLAY_LATEST: context = { 'posts': Post.latest_posts() } sidebar.content = render_to_string('config/blocks/sidebar_posts.html', context) elif sidebar.display_type == SideBar.DISPLAY_HOT: context = { 'posts': Post.hot_posts() } sidebar.content = render_to_string('config/blocks/sidebar_posts.html', context) elif sidebar.display_type == SideBar.DISPLAY_COMMENT: context = { 'comments': Comment.objects.filter(status=Comment.STATUS_NORMAL) } sidebar.content = render_to_string('config/blocks/sidebar_comments.html', context) sidebars.append(sidebar) context = { 'category': category, 'tag': tag, 'post_list': post_list, 'sidebars': sidebars, } context.update(Category.get_navs()) return render(request, 'blog/list.html', context=context) def post_detail(request, post_id): # return HttpResponse('detail') try: post = Post.objects.get(id=post_id) except Post.DoesNotExist: post = None sidebars = [] for sidebar in SideBar.get_all(): if sidebar.display_type == SideBar.DISPLAY_HTML: pass elif sidebar.display_type == SideBar.DISPLAY_LATEST: context = { 'posts': Post.latest_posts() } sidebar.content = render_to_string('config/blocks/sidebar_posts.html', context) elif sidebar.display_type == SideBar.DISPLAY_HOT: context = { 'posts': Post.hot_posts() } sidebar.content = render_to_string('config/blocks/sidebar_posts.html', context) elif sidebar.display_type == SideBar.DISPLAY_COMMENT: context = { 'comments': Comment.objects.filter(status=Comment.STATUS_NORMAL) } sidebar.content = render_to_string('config/blocks/sidebar_comments.html', context) sidebars.append(sidebar) context = { 'post': post, 'sidebars': sidebars, } context.update(Category.get_navs()) return render(request, 'blog/detail.html', context=context)
上面代码在view中通过判断sidebar.display_type的类型来设置sidebar.content,在list.html和detail.html中还是调用sidebar.content.
The text was updated successfully, but these errors were encountered:
No branches or pull requests
说明:
config/blocks/sidebar_posts.html和config/blocks/sidebar_comments.html模板文件跟Model层封装的一样。
修改blog/views.py为:
上面代码在view中通过判断sidebar.display_type的类型来设置sidebar.content,在list.html和detail.html中还是调用sidebar.content.
The text was updated successfully, but these errors were encountered: