Skip to content
New issue

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

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

Open
MaoningGuan opened this issue Jun 14, 2020 · 0 comments

Comments

@MaoningGuan
Copy link

MaoningGuan commented Jun 14, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant