diff --git a/selfblog/blog/models.py b/selfblog/blog/models.py index b2e4e1d..4c62be8 100644 --- a/selfblog/blog/models.py +++ b/selfblog/blog/models.py @@ -1,8 +1,9 @@ -#coding:utf-8 +# coding:utf-8 from datetime import datetime from django.db import models from django.contrib.auth.models import User +from django.utils.functional import cached_property from selfblog import settings from utils.cache import cache_decorator @@ -77,6 +78,16 @@ def tags_list(self): def get_absolute_url(self): return '%s/%s.html' % (settings.DOMAIN, self.alias) + @cached_property + def next_post(self): + # 下一篇 + return Post.objects.filter(id__gt=self.id, status=0).order_by('id').first() + + @cached_property + def prev_post(self): + # 前一篇 + return Post.objects.filter(id__lt=self.id, status=0).first() + @classmethod @cache_decorator(1*60) def get_recently_posts(cls, num): diff --git a/selfblog/blog/templates/detail.html b/selfblog/blog/templates/detail.html index b9288d6..bad2b12 100644 --- a/selfblog/blog/templates/detail.html +++ b/selfblog/blog/templates/detail.html @@ -77,15 +77,15 @@

【上一篇】 - {% if prev_post %} - {{ prev_post.title }} + {% if post.prev_post %} + {{ post.prev_post.title }} {% else %} 没有了 {% endif %}
【下一篇】 - {% if next_post %} - {{ next_post.title }} + {% if post.next_post %} + {{ post.next_post.title }} {% else %} 没有了 {% endif %} diff --git a/selfblog/blog/views.py b/selfblog/blog/views.py index 60538ad..a709606 100644 --- a/selfblog/blog/views.py +++ b/selfblog/blog/views.py @@ -1,7 +1,8 @@ -#coding:utf-8 +# coding:utf-8 import logging from django.db.models import Q +from django.db.models import F from django.core.paginator import Paginator from django.views.generic import ListView, DetailView from django.shortcuts import render @@ -129,8 +130,7 @@ def get(self, request, *args, **kwargs): visited_ips = cache.get(self.object.id, []) if ip not in visited_ips: - self.object.view_times += 1 - self.object.save() + Post.objects.filter(id=self.object.id).update(view_times=F('view_times')+1) visited_ips.append(ip) @@ -143,7 +143,7 @@ def get(self, request, *args, **kwargs): return self.render_to_response(context) def set_lru_read(self, ip, post): - #保存别人正在读 + # 保存别人正在读 lru_views = cache.get('lru_views') if not lru_views: lru_views = LRUCacheDict(max_size=10, expiration=FIF_MIN) @@ -155,27 +155,11 @@ def set_lru_read(self, ip, post): def get_context_data(self, **kwargs): context = super(PostDetailView, self).get_context_data(**kwargs) - post = self.get_object() - next_id = post.id + 1 - prev_id = post.id - 1 - - try: - next_post = self.queryset.get(id=next_id) - except Post.DoesNotExist: - next_post = None - - try: - prev_post = self.queryset.get(id=prev_id) - except Post.DoesNotExist: - prev_post = None - - context['next_post'] = next_post - context['prev_post'] = prev_post context['lru_views'] = cache.get('lru_views', {}).items() context['cur_user_ip'] = self.cur_user_ip - context['related_posts'] = post.related_posts + context['related_posts'] = self.object.related_posts return context