38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
|
from django.http import HttpResponse
|
||
|
from django.shortcuts import render
|
||
|
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
|
||
|
from .models import News
|
||
|
|
||
|
|
||
|
def index(request):
|
||
|
category = request.GET.get('category')
|
||
|
if category:
|
||
|
news = News.objects.filter(category=category).order_by('-created')
|
||
|
else:
|
||
|
news = News.objects.all().order_by('-created')
|
||
|
paginator = Paginator(news, 12)
|
||
|
|
||
|
p = int(request.GET.get('page', 1))
|
||
|
try:
|
||
|
contacts = paginator.page(p)
|
||
|
except PageNotAnInteger:
|
||
|
contacts = paginator.page(1)
|
||
|
except EmptyPage:
|
||
|
contacts = paginator.page(paginator.num_pages)
|
||
|
return render(request, 'news/index.html', {'contacts': contacts, 'category': category, 'p': p})
|
||
|
|
||
|
|
||
|
def details(request):
|
||
|
category = request.GET.get('category')
|
||
|
id = request.GET.get('id')
|
||
|
n = News.objects.get(id=id)
|
||
|
n.hit_count += 1
|
||
|
n.save()
|
||
|
return render(request, 'news/details.html', {'n': n, 'category': category})
|
||
|
|
||
|
|
||
|
def search(request):
|
||
|
title = request.GET['title']
|
||
|
contacts = News.objects.filter(title__contains=title).order_by('-created')
|
||
|
return render(request, 'news/index.html', {'contacts': contacts})
|