the5fire / typeidea

Django企业开发实战对应项目代码
http://django-practice-book.com/
Other
439 stars 214 forks source link

参考代码:在View层封装SideBar《Django企业开发实战》2020年3月河北第6次印刷第153页 #43

Open MaoningGuan opened 4 years ago

MaoningGuan commented 4 years ago

说明: 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.