From ed92b5d3e189cdc634048e1ba7b4cd6a31d11dcd Mon Sep 17 00:00:00 2001
From: andrewlalis
Date: Tue, 2 Oct 2018 14:48:26 +0200
Subject: [PATCH] Added basic search function.
---
backend/RateMyCourse/urls.py | 3 ++
.../postings/templates/postings/index.html | 30 +++++++++++++------
backend/postings/views.py | 8 ++++-
3 files changed, 31 insertions(+), 10 deletions(-)
diff --git a/backend/RateMyCourse/urls.py b/backend/RateMyCourse/urls.py
index cc3e5cb..581fd0f 100644
--- a/backend/RateMyCourse/urls.py
+++ b/backend/RateMyCourse/urls.py
@@ -24,6 +24,9 @@ urlpatterns = [
# / routes to index.html
path('', views.index, name='homepage'),
+ # /?search_query=xxx routes to the homepage, with displaying some basic results.
+ path('
- Click one of the links below to view a list of all those entities.
-
-
- -
+ {# First section for searching our database. #}
+
- -
Courses
-
-
+
+
+
+ {# Second section for displaying results, or whatever should be shown first. #}
+ {% if results %}
+
+
+ {% for entity in results %}
+ - {{ entity.name }}
+ {% endfor %}
+
+
+ {% endif %}
+
{% endblock %}
\ No newline at end of file
diff --git a/backend/postings/views.py b/backend/postings/views.py
index 58288a1..61c25b9 100644
--- a/backend/postings/views.py
+++ b/backend/postings/views.py
@@ -5,8 +5,14 @@ from postings.models import *
# Create your views here.
# The view for the homepage, or index.html
+# There is an optional 'search_query GET parameter, which, if provided, gives the template a 'results' variable.
def index(request):
- return render(request, 'postings/index.html')
+ search_query = request.GET.get('search_query', None)
+ results = None
+ if search_query:
+ results = RateableEntity.objects.filter(name__icontains=search_query)
+
+ return render(request, 'postings/index.html', {'results': results})
# The view for a listing of universities.
def universities(request):