views.py 1.34 KB
Newer Older
1
2
3
from django.shortcuts import render, get_object_or_404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.db.models import Q
luxcem's avatar
luxcem committed
4

5
from representatives.models import Representative
luxcem's avatar
luxcem committed
6
7


8
9
10
11
12
13
14
15
16
17
18
19
20
def index(request):
    context = {}
    if request.GET.get('search'):
        search = request.GET.get('search')
        representative_list = Representative.objects.filter(
            Q(full_name__icontains=search)
        )
        queries_without_page = request.GET.copy()
        if 'page' in queries_without_page:
            del queries_without_page['page']
        context['queries'] = queries_without_page
    else:
        representative_list = Representative.objects.all()
luxcem's avatar
luxcem committed
21

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    paginator = Paginator(representative_list, 5)

    page = request.GET.get('page')
    try:
        representatives = paginator.page(page)
    except PageNotAnInteger:
        representatives = paginator.page(1)
    except EmptyPage:
        representatives = paginator.page(paginator.num_pages)

    context['representatives'] = representatives
    return render(
        request,
        'memopol_representatives/list.html',
        context
    )


def view(request, num):
    representative = get_object_or_404(Representative, pk=num)

    return render(
        request,
        'memopol_representatives/view.html',
        {'representative': representative}
    )