728x90
models.py
hits = models.PositiveIntegerField(default=1, verbose_name='조회수')
views.py
def detail(request, id):
detail_data = get_object_or_404(Book, pk = id)
comments = Comment.objects.filter(book_id=id, comment_id__isnull=True)
re_comments = []
for comment in comments:
re_comments += list(Comment.objects.filter(comment_id=comment.id))
form = CommentForm()
response = render(request, 'detail.html' ,{'data' : detail_data, 'comments' : comments, 're_comments' : re_comments, 'form':form})
#조회수 기능 (쿠키 이용)
expire_date, now = datetime.now(), datetime.now()
expire_date += timedelta(days=1)
expire_date = expire_date.replace(hour=0, minute=0, second=0, microsecond=0)
expire_date -= now
max_age = expire_date.total_seconds()
cookie_value = request.COOKIES.get('hitblog', '_')
if f'_{id}_' not in cookie_value:
cookie_value += f'{id}_'
response.set_cookie('hitblog', value=cookie_value, max_age=max_age, httponly=True)
detail_data.hits += 1
detail_data.save()
return response
detail.html
<h5 class=hits>🖱조회수 : {{ data.hits }}</h5>
728x90
'기타 > Django & Web' 카테고리의 다른 글
[django] 네이버 도서 api 이용하기 (0) | 2021.09.01 |
---|---|
[django/오류] CSRF verification failed. Request aborted (0) | 2021.09.01 |
[ Django ] 네이버 API 활용하기 (0) | 2021.08.23 |
[bootstap] bootstrap card align horizontal (0) | 2021.08.05 |
[django] OperationalError at[] no such table: [] (0) | 2021.08.05 |