一、添加上一篇和下一篇。

1.思路:模型中博客按发表时间排序。取得所有大于当前博客发表日期列表中的最后一篇为上一篇博客。取得所有小于当前博客发表日期的列表中的第一篇就为下一篇。在views中用filter()取得以上两个值。传入到模版,在模板中判断显示。

2.由于filter()函数中不能使用“=,>,<”等符号,所有设计了一些方法代替,查询条件判断。

__gt,__gte,__lt,__lte 用于数字日期比较,__contains,__startwith,__endwith用于字符(前面加i忽略大小写)。不等于使用:

如:Blog.objects.exclude(pk=3)

双划线日期拓展:Blog.objects.filter(created_time__=2018) 可以查询到2018年分表博客。

3.修改views.py结果。

1 def blog_detail(request,blog_pk):
2     context={}
3     blog=get_object_or_404(Blog,pk=blog_pk)
4     context['blog']=blog
5     context['previous_blog']=Blog.objects.filter(created_time__gt=blog.created_time).last()
#取得所有大于当前博客创建时间所有博客中的最后一条。
6 context['next_blog']=Blog.objects.filter(created_time__lt=blog.created_time).first() 7 return render_to_response('myblog/blog_detail.html',context)
 #也可以用[0]取得列表的第一条。

4.修改blog_detail.html。

 1 <div class="blog-content"> {{blog.content}} </div>
 2 <div class="blog-more">
 3     <p>上一篇:
 4         {% if previous_blog %}
 5             <a href="{% url 'blog_detail' previous_blog.pk %}" >{{previous_blog.title}}</a>
 6         {% else %}
 7             没有了
 8         {% endif %}
 9        下一篇:
10         {% if next_blog %}
11             <a href="{% url 'blog_detail' next_blog.pk %}" >{{next_blog.title}}  </a> 
12         {% else %}
13             没有了
14         {% endif %}
15     </p>
16 </div>

 二、按月分类

1.思路:在blog_list.html中建立一个时间归档块,然后写时间归档url,写时间归档函数,写时间归档页面。

使用dates()函数在views的blog_list函数取得时间值。 dates(field,kind.order='ACS')函数使用方法可以在boots中查找。

context['blog_dates']=Blog.objects.dates('created_time','month',order='DESC')    

2.blog_list.html中添加页面。

<div class="panel panel-default">
    <div class="panel-heading">日期归档</div>
    <div class="panel-body">
        <ul class="blog-types" > 
            {% for blog_date in blog_dates %}
                <li><a href="{% url 'blogs_with_date' blog_date.year blog_date.month %}">   
                   {{blog_date | date:"Y年m月"}}
                </a></li>
            {% empty %}
                 <li>暂无分类</li>
            {% endfor %}
         </ul>    
     </div>
</div>

3.新建路径path('date/<int:year>/<int:month>',views.blogs_with_date,name="blogs_with_date")

4.复制blog_list.py写blogs_with_date.py方法。

 1 def blog_list(request):    #
 2     blogs_all_list=Blog.objects.all()  
 3     paginator=Paginator(blogs_all_list,settings.EACH_PAGE_BLOGS_NUMBER)  #
 4     page_num=request.GET.get('page',1) 
 5     page_of_blogs=paginator.get_page(page_num)    
 6     
 7     currentr_page_num=page_of_blogs.number
 8     page_range=list(range(max(currentr_page_num-3,1),currentr_page_num))+list(range(currentr_page_num,min(paginator.num_pages,currentr_page_num+3)+1))
 9     
10     if page_range[0]-1 >=2:
11         page_range.insert(0,'...')
12     if paginator.num_pages - page_range[-1] >=2:
13         page_range.append('...')
14     
15     if page_range[0]!=1:
16         page_range.insert(0,1)
17     if page_range[-1]!=paginator.num_pages:
18         page_range.append(paginator.num_pages)
19         
20     context={}
21     context['blogs']=page_of_blogs.object_list
22     context['page_of_blogs']=page_of_blogs    
23     context['page_range']=page_range    
24     context['blog_types']=BlogType.objects.all()
25     context['blogs_count']=Blog.objects.all().count()
26     
27     context['blog_dates']=Blog.objects.dates('created_time','month',order='DESC')
28     
29     return render_to_response('myblog/blog_list.html',context)

5.复制blogs_type.html页面创建blogs_with_date.html页面,传入参数。

{% extends 'myblog/blog_list.html' %}
{% block title %}{{ blogs_with_date }}{% endblock %}
{% block blog_list_title %}
     日期归档:{{ blogs_with_date}}&ensp; <a href="{% url 'myblog' %}">查看全部博客</a>
{% endblock %}

 三、优化views.py,将各个方法中相同的部分提出建立一个新的函数。然后将值传入以简化代码。完整代码:

 1 from django.shortcuts import render_to_response,get_object_or_404
 2 from django.core.paginator import Paginator
 3 from django.conf import settings
 4 from .models import Blog,BlogType
 5 
 6 def get_blog_list_common_date(request,blogs_all_list):
 7     paginator=Paginator(blogs_all_list,settings.EACH_PAGE_BLOGS_NUMBER)  #
 8     page_num=request.GET.get('page',1) 
 9     page_of_blogs=paginator.get_page(page_num)    
10     currentr_page_num=page_of_blogs.number
11     page_range=list(range(max(currentr_page_num-3,1),currentr_page_num))+list(range(currentr_page_num,min(paginator.num_pages,currentr_page_num+3)+1))
12     
13     if page_range[0]-1 >=2:
14         page_range.insert(0,'...')
15     if paginator.num_pages - page_range[-1] >=2:
16         page_range.append('...')
17     
18     if page_range[0]!=1:
19         page_range.insert(0,1)
20     if page_range[-1]!=paginator.num_pages:
21         page_range.append(paginator.num_pages)
22         
23     context={}
24     context['blogs']=page_of_blogs.object_list
25     context['page_of_blogs']=page_of_blogs    
26     context['page_range']=page_range    
27     context['blog_types']=BlogType.objects.all()
28     context['blogs_count']=Blog.objects.all().count()    
29     context['blog_dates']=Blog.objects.dates('created_time','month',order='DESC')
30     
31     return context  
32 
33 def blog_list(request):    #
34     blogs_all_list=Blog.objects.all()  
35     context=get_blog_list_common_date(request,blogs_all_list)        
36     return render_to_response('myblog/blog_list.html',context)    
37     
38 def blogs_with_type(request,blog_type_pk):
39     blog_type=get_object_or_404(BlogType,pk=blog_type_pk)  
40     blogs_all_list=Blog.objects.filter(blog_type=blog_type)  
41     
42     context=get_blog_list_common_date(request,blogs_all_list)
43     context['blog_type']=blog_type 
44     return render_to_response('myblog/blogs_with_type.html',context)
45 
46 def blogs_with_date(request,year,month):
47     blogs_all_list=Blog.objects.filter(created_time__year=year,created_time__month=month) 
48     context=get_blog_list_common_date(request,blogs_all_list)
49     
50     context['blogs_with_date']='%s年%s月' %(year,month)
51     return render_to_response('myblog/blogs_with_date.html',context)
52     
53 def blog_detail(request,blog_pk):
54     context={}
55     blog=get_object_or_404(Blog,pk=blog_pk)
56     context['blog']=blog
57     context['previous_blog']=Blog.objects.filter(created_time__gt=blog.created_time).last()
58     context['next_blog']=Blog.objects.filter(created_time__lt=blog.created_time).first()
59     return render_to_response('myblog/blog_detail.html',context) 

 

posted on 2018-11-27 00:12  南飞雁ht  阅读(148)  评论(0)    收藏  举报