当前位置: 首页 > 后端技术 > Python

Django20200408博客开发008

时间:2023-03-26 15:04:23 Python

功能增加:如图。实现方法:filter方法,方法如下:修改views.py中的内容:然后修改blog\_detail.html的显示结构和内容:效果如图:但是现在我希望如果上一篇和下一篇如果没有就没有写了,下篇也一样,希望上下篇都能点到,修改blog\_detail.html内容如下:效果如下:修改blog.css中新增blog-more的样式:div.blog-more{margin-top:1em;}新增功能:新增分类,可按年/月分类。点击后,对应时间段的所有博文都会先写入url的匹配地址:localhost.8000/date/2019/3访问2019年3月的所有博文修改blog\_list.html中的内容:然后从数据库中获取数据:修改views.py中的blog\_list函数:然后修改blog\_list.html中的datearchive内容:效果如下:去掉之前的点:base.css,把日期到超链接:base.css*{margin:0;padding:0;}body{margin-top:70px!important;}ul{list-style-type:none!important;}效果如图:bug:datearchive还是有些点,删除失败!点击日期存档内容后添加查看功能:defblogs_with_date(request,year,month):"""localhost.8000/date/2019/3查询2019年3月所有博文,复制以上内容,修改partialcontent"""context={}#根据需要的时间查询博客内容blogs_all_list=Blog.objects.filter(created_time__year=year,created_time__month=month)paginator=Paginator(blogs_all_list,settings.EACH_PAGE_BLOGS_NUMBER)getpage_num.=request.GET'page',1)#获取url的页码参数(GET请求)page_of_blogs=paginator.get_page(page_num)currentr_page_num=page_of_blogs.number#获取当前页码#获取当前页前后2页的页码范围page_range=list(range(max(currentr_page_num-2,1),currentr_page_num))+\range(currentr_page_num,min(currentr_page_num+2,paginator.num_pages)+1))#添加省略页码标记ifpage_range[0]-1>=2:page_range.insert(0,'...')如果是分页器。num_pages-page_range[-1]>=2:page_range.append('...')#添加第一页和最后一页ifpage_range[0]!=1:page_range.insert(0,1)ifpage_range[-1]!=paginator.num_pages:page_range.append(paginator.num_pages)context={}context['blogs_with_date']='%syear%smonth'%(year,month)#第15节新增context['blogs']=page_of_blogs.object_listcontext['page_of_blogs']=page_of_blogscontext['page_range']=page_rangecontext['blog_types']=BlogType.objects.all()context['blog_dates']=Blog.objects.dates('created_time','蒙th',order='DESC')returnrender(request,'blog/blogs_with_date.html',context)然后创建blogs\_with\_date.html文件(与blogs\_with\_type.html文件基本相同,唯一不同的是将左上角的博客列表改为“xx年xx月博客”;{%extends'blog/blog_list.html'%}{%blocktitle%}{{blog_type.type_name}}{%endblock%}{%blockblog_list_title%}日期分类:{{blogs_with_date}}查看所有博客{%endblock%}现在可以点击里面的内容了datearchive,但是代码重复内容太多,需要精简:把共同的内容放在一个函数里,其他的都调用!views.py的整个代码如下:fromdjango.shortcutsimportrender,get_object_or_404fromdjango.core.paginator从django.conf导入分页器从.models导入博客,BlogTypedefget_blogs_list_common_data(请求,blogs_all_list):paginator=Paginator(blogs_all_list,settings.EACH_PAGE_BLOGS_NUMBER)page_num=request.GET.get('page',1)#获取url的页码参数(GET请求)page_of_blogs=paginator.get_page(page_num)currentr_page_num=page_of_blogs.number#获取当前页码#获取当前页码当前页前后2页的页码范围page_range=list(range(max(currentr_page_num-2,1),currentr_page_num))+\list(range(currentr_page_num,min(currentr_page_num+2,paginator.num_pages)+1))#添加省略页码标记ifpage_range[0]-1>=2:page_range.insert(0,'...')#0表示位置,后面的省略号表示要插入的内容ifpaginator.num_pages-page_range[-1]>=2:page_range.append('...')#使用append在页码末尾插入省略号#添加第一页和最后一页ifpage_range[0]!=1:page_range.insert(0,1)ifpage_range[-1]!=paginator.num_pages:page_range.append(paginator.num_pages)context={}context['blogs']=page_of_blogs.object_listcontext['page_of_blogs']=page_of_blogscontext['page_range']=page_rangecontext['blog_types']=BlogType.objects。all()context['blog_dates']=Blog.objects.dates('created_time','month',order="DESC")返回contextdefblog_list(request):blogs_all_list=Blog.objects.all()context=get_blogs_list_common_data(请求,blogs_all_list)returnrender(request,'blog/blog_list.html',context)defblogs_with_type(request,blog_type_pk):blog_type=get_object_or_404(BlogType,pk=blog_type_pk)blogs_all_list=Blog.objects.filter(blog_type=blog_type)context_comlogs_common_list_bk(请求,blogs_all_list)context['blog_type']=blog_typereturnrender(request,'blog/blogs_with_type.html',context)defblogs_with_date(request,year,month):"""localhost.8000/date/2019/3来查询2019年3月所有博文,复制以上内容,修改部分内容"""blogs_all_list=Blog.objects.filter(created_time__year=year,created_time__month=month)context=get_blogs_list_common_data(request,blogs_all_list)context['blogs_with_date']='%syear%smonth'%(year,month)#15节新增returnrender(request,'blog/blogs_with_date.html',context)defblog_detail(request,blog_pk):"""打开某个博文后的显示内容"""context={}blog=get_object_or_404(Blog,pk=blog_pk)#Lastblog:所有创建时间大于当前打开的博客创建时间的博文,使用last获取上一篇博文context['previous_blog']=Blog.objects.filter(created_time__gt=blog.created_time).last()#下一篇博文同理context['next_blog']=Blog.objects.filter(created_time__lt=blog.created_time).first()context['blog']=blogreturnrender(request,'blog/blog_detail.html',上下文)