From e08dcd0cd0d71f65cb6fad387cbe2956ed55f0e4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fr=C3=A9d=C3=A9ric=20Perrin?= Date: Thu, 3 Nov 2016 19:05:25 +0000 Subject: [PATCH] Add a search page --- quotes/search.py | 7 +++++++ quotes/templates/quotes/search.html | 19 +++++++++++++++++++ quotes/urls.py | 5 +++++ quotes/views.py | 9 ++++++++- 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 quotes/search.py create mode 100644 quotes/templates/quotes/search.html diff --git a/quotes/search.py b/quotes/search.py new file mode 100644 index 0000000..876debd --- /dev/null +++ b/quotes/search.py @@ -0,0 +1,7 @@ +from quotes.models import Quote + +def search(pattern): + results = {} + + results['quotes'] = Quote.objects.filter(text__contains=pattern) + return results diff --git a/quotes/templates/quotes/search.html b/quotes/templates/quotes/search.html new file mode 100644 index 0000000..41fc237 --- /dev/null +++ b/quotes/templates/quotes/search.html @@ -0,0 +1,19 @@ +{% extends 'quotes/base.html' %} + +{% block title %}Searching for quotes{% endblock %} + +{% block body %} + +{% for quote in quotes %} + {% include "quotes/display.html" with quote=quote %} +{% endfor %} + +
+ +
+ {% csrf_token %} + Search: + +
+ +{% endblock %} diff --git a/quotes/urls.py b/quotes/urls.py index 8864db0..13323a4 100644 --- a/quotes/urls.py +++ b/quotes/urls.py @@ -4,11 +4,16 @@ from . import views urlpatterns = [ url(r'^$', views.random, name='random'), + url(r'^random$', views.random, name='random'), url(r'^random/$', views.random, name='random'), + url(r'^show/(?P[0-9]+)/$', views.onequote, name="onequote"), url(r'^tag/(?P[0-9]+)/$', views.tags, name="tags"), url(r'^author/(?P[0-9]+)/$', views.author, name="author"), url(r'^work/(?P[0-9]+)/$', views.work, name="work"), + + url(r'^search/$', views.searchpage, name="search"), + url(r'^all/$', views.all, name="all"), ] diff --git a/quotes/views.py b/quotes/views.py index 4e66378..2aca193 100644 --- a/quotes/views.py +++ b/quotes/views.py @@ -2,7 +2,8 @@ from django.shortcuts import render from random import randint -from .models import Author, Work, Quote, QuoteTag +from quotes.models import Author, Work, Quote, QuoteTag +import quotes.search as search # Create your views here. def onequote(request, quote_id): @@ -36,3 +37,9 @@ def all(request): quotes = Quote.objects.all() context = { 'quotes' : quotes } return render(request, 'quotes/all.html', context) + +def searchpage(request): + results = {} + if 'q' in request.POST: + results = search.search(request.POST['q']) + return render(request, 'quotes/search.html', results) -- 2.43.0