]> gitweb.fperrin.net Git - djsite.git/commitdiff
Add a search page
authorFrédéric Perrin <frederic.perrin@resel.fr>
Thu, 3 Nov 2016 19:05:25 +0000 (19:05 +0000)
committerFrédéric Perrin <frederic.perrin@resel.fr>
Thu, 3 Nov 2016 19:05:25 +0000 (19:05 +0000)
quotes/search.py [new file with mode: 0644]
quotes/templates/quotes/search.html [new file with mode: 0644]
quotes/urls.py
quotes/views.py

diff --git a/quotes/search.py b/quotes/search.py
new file mode 100644 (file)
index 0000000..876debd
--- /dev/null
@@ -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 (file)
index 0000000..41fc237
--- /dev/null
@@ -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 %}
+
+<hr />
+
+<form action="{% url 'quotes:search' %}" method="post">
+  {% csrf_token %}
+  Search: <input type="text" name="q" />
+  <input type="submit" value="Vote" />
+</form>
+
+{% endblock %}
index 8864db0ce36ce98f2fea743bf80a776adb939919..13323a49bd41dfbaccd39d1eaa0607cef06b8613 100644 (file)
@@ -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<quote_id>[0-9]+)/$', views.onequote, name="onequote"),
     url(r'^tag/(?P<tag_id>[0-9]+)/$', views.tags, name="tags"),
     url(r'^author/(?P<author_id>[0-9]+)/$', views.author, name="author"),
     url(r'^work/(?P<work_id>[0-9]+)/$', views.work, name="work"),
+
+    url(r'^search/$', views.searchpage, name="search"),
+
     url(r'^all/$', views.all, name="all"),
 ]
index 4e66378bc6ea8cc8ad5acf7937afae14c5002350..2aca193501b703c79ac77177000fbfc92102ae05 100644 (file)
@@ -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)