def search(request): """Search for books. GET: q -- the query string lucky -- whether the lucky button is clicked Renders library/searchResult.html with: q -- the query string result -- search result, as a list of books """ q = request.GET.get('q', '') lucky = 'lucky' in request.GET if lucky: try: book = choice(Book.objects.all()) return redirect('library:book', book.id) except IndexError as err: pass return render(request, 'library/searchResult.html', { 'q': q, 'result': Book.search(q), })
def search_by_query(self): """Function to search book by query""" query = self.search_query.get("q") if query: self.db_query = Book.search(query) return self