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