Danny830x

searchview.py

May 1st, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.25 KB | None | 0 0
  1. # If no query is entered, will bring you to blank search screen. Otherwise will pull results from keyword, and results from content and display in search.html
  2. @login_required(login_url='/auth/login/')
  3. def search(request):
  4.     query = request.GET.get('q', '')
  5.     results = []
  6.     notecard_list = []
  7.     if query:
  8.         if query == "*":
  9.             results = Notecard.objects.filter(section__semester__user=request.user)
  10.         else:
  11.             results = Notecard.objects.filter(Q(section__semester__user=request.user), Q(notecard_body__icontains=query)|Q(notecard_name__icontains=query))
  12.         paginator = Paginator(results, 6)
  13.            
  14.         try:
  15.             page = int(request.GET.get('page', '1'))
  16.         except ValueError:
  17.             page = 1
  18.                
  19.         try:
  20.             notecard_list = paginator.page(page)
  21.         except (EmptyPage, InvalidPage):
  22.             notecard_list = paginator.page(paginator.num_pages)
  23.     return list_detail.object_list(
  24.         request,
  25.         queryset = Notecard.objects.filter(section__semester__user=request.user),
  26.         template_name = "notecards/search.html",
  27.         template_object_name = "results",
  28.         extra_context = {"results": results, "notecard_list": notecard_list,},
  29.     )
Advertisement
Add Comment
Please, Sign In to add comment