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

View File

@ -3,15 +3,27 @@
{# The homepage for the website. #} {# The homepage for the website. #}
{% block content %} {% block content %}
<p> {# First section for searching our database. #}
Click one of the links below to view a list of all those entities. <section>
</p> <form method="GET" action="/">
<ul> <input type="text" name="search_query">
<li> <button type="submit">Search</button>
</form>
<nav>
<a href="/universities">Universities</a> <a href="/universities">Universities</a>
</li>
<li>
<a href="/courses">Courses</a> <a href="/courses">Courses</a>
</li> </nav>
</ul> </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 %} {% endblock %}

View File

@ -5,8 +5,14 @@ from postings.models import *
# Create your views here. # Create your views here.
# The view for the homepage, or index.html # 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): 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. # The view for a listing of universities.
def universities(request): def universities(request):