Added basic search function.

This commit is contained in:
Andrew Lalis 2018-10-02 14:48:26 +02:00
parent 714f3241ce
commit ed92b5d3e1
3 changed files with 31 additions and 10 deletions

View File

@ -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('<str:search_query', views.index, name='homepage'),
# /universities routes to a list of universities.
path('universities', views.universities, name='universities_list'),

View File

@ -3,15 +3,27 @@
{# The homepage for the website. #}
{% block content %}
<p>
Click one of the links below to view a list of all those entities.
</p>
<ul>
<li>
{# First section for searching our database. #}
<section>
<form method="GET" action="/">
<input type="text" name="search_query">
<button type="submit">Search</button>
</form>
<nav>
<a href="/universities">Universities</a>
</li>
<li>
<a href="/courses">Courses</a>
</li>
</ul>
</nav>
</section>
{# Second section for displaying results, or whatever should be shown first. #}
{% if results %}
<section>
<ul>
{% for entity in results %}
<li>{{ entity.name }}</li>
{% endfor %}
</ul>
</section>
{% endif %}
{% endblock %}

View File

@ -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):