Py学习  »  问与答

请教如何实现博客的上一篇、下一篇文章的功能

往事如翔飞机王-weibo • 11 年前 • 5521 次点击  

需求是这样的:在博客每一篇文章的显示页面(show_blog.html)的最下面显示上一篇文章的链接、下一篇文章的链接,假如没有上一篇(或下一篇)文章时就显示“没有文章了”。我已经写了一个独立control_blog.html实现这个功能了,但现在困惑怎样把control_blog.html包含到show_blog.html中。我知道html可以用《object》《/object》来调用另一个html,但是怎样把blog的id传给control_blog.html所对应view呢。感觉我这种方法进入了一个死胡同了,大牛们有没有其他实现这个需求的方法?感谢!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/429
 
5521 次点击  
文章 [ 2 ]  |  最新文章 11 年前
BeginMan
Reply   •   1 楼
BeginMan    11 年前
def get_neighbour(id):
"""
功能说明:获取上一篇、下一篇
id:对应博客id

"""

blog_list = Blog.objects.values_list('id', flat=True).order_by('id')
dic = {}
blog_list = list(blog_list)
if blog_list:
    id_index = blog_list.index(id)  # 当前id的索引
    pre = next = 0

    if len(blog_list)>1:
        if id_index != 0 and id_index !=len(blog_list)-1:      # 如果不是第一篇或最后一篇
            pre = blog_list[id_index-1]
            next = blog_list[id_index+1]
        else:
            if id_index == 0:       # 第一篇
                next = blog_list[id_index+1]
            if id_index == len(blog_list)-1:    # 最后一篇
                pre = blog_list[id_index-1]
    elif len(blog_list) == 1:
        pre, next = 0, 0
    dic = {'pre': pre, 'next': next}

return dic

def tagsCloud():
 """标签云"""

 tags = Tags.objects.all()
 tagscloud = []
 for obj in tags:
    size = random.randint(12,50)        # 随机字体
    R = random.randint(0,254)
    G = random.randint(0,254)
    B = random.randint(0,254)       # 没有白色
    RGB = 'rgb(%d,%d,%d)' %(R,G,B)      # 随机颜色
    dic = {}
    dic['name'] = obj.name
    dic['id'] = obj.id
    dic['size'] = size
    dic['rgb'] = RGB
    tagscloud.append(dic)
 return tagscloud
Py站长
Reply   •   2 楼
Py站长    11 年前

可以为你的 show_blog.html 写一个中间件,可以拦截 control_blog.html 参数中的 blog_id