##// END OF EJS Templates
Search posts by text and title
neko259 -
r1849:d9dcb07b default
parent child Browse files
Show More
@@ -1,44 +1,45 b''
1 from django.shortcuts import render
1 from django.shortcuts import render
2 from django.views.generic import View
2 from django.views.generic import View
3 from django.db.models import Q
3
4
4 from boards.abstracts.paginator import get_paginator
5 from boards.abstracts.paginator import get_paginator
5 from boards.forms import SearchForm, PlainErrorList
6 from boards.forms import SearchForm, PlainErrorList
6 from boards.models import Post
7 from boards.models import Post
7
8
8
9
9 MIN_QUERY_LENGTH = 3
10 MIN_QUERY_LENGTH = 3
10 RESULTS_PER_PAGE = 10
11 RESULTS_PER_PAGE = 10
11
12
12 FORM_QUERY = 'query'
13 FORM_QUERY = 'query'
13
14
14 CONTEXT_QUERY = 'query'
15 CONTEXT_QUERY = 'query'
15 CONTEXT_FORM = 'form'
16 CONTEXT_FORM = 'form'
16 CONTEXT_PAGE = 'page'
17 CONTEXT_PAGE = 'page'
17
18
18 REQUEST_PAGE = 'page'
19 REQUEST_PAGE = 'page'
19
20
20 __author__ = 'neko259'
21 __author__ = 'neko259'
21
22
22 TEMPLATE = 'search/search.html'
23 TEMPLATE = 'search/search.html'
23
24
24
25
25 class BoardSearchView(View):
26 class BoardSearchView(View):
26 def get(self, request):
27 def get(self, request):
27 params = dict()
28 params = dict()
28
29
29 form = SearchForm(request.GET, error_class=PlainErrorList)
30 form = SearchForm(request.GET, error_class=PlainErrorList)
30 params[CONTEXT_FORM] = form
31 params[CONTEXT_FORM] = form
31
32
32 if form.is_valid():
33 if form.is_valid():
33 query = form.cleaned_data[FORM_QUERY]
34 query = form.cleaned_data[FORM_QUERY]
34 if len(query) >= MIN_QUERY_LENGTH:
35 if len(query) >= MIN_QUERY_LENGTH:
35 results = Post.objects.filter(text__icontains=query)\
36 results = Post.objects.filter(Q(text__icontains=query) |
36 .order_by('-id')
37 Q(title__icontains=query)).order_by('-id')
37 paginator = get_paginator(results, RESULTS_PER_PAGE)
38 paginator = get_paginator(results, RESULTS_PER_PAGE)
38
39
39 page = int(request.GET.get(REQUEST_PAGE, '1'))
40 page = int(request.GET.get(REQUEST_PAGE, '1'))
40
41
41 params[CONTEXT_PAGE] = paginator.page(page)
42 params[CONTEXT_PAGE] = paginator.page(page)
42 params[CONTEXT_QUERY] = query
43 params[CONTEXT_QUERY] = query
43
44
44 return render(request, TEMPLATE, params)
45 return render(request, TEMPLATE, params)
General Comments 0
You need to be logged in to leave comments. Login now