Skip to content

Commit 81a9b9d

Browse files
committed
Show message if no results
1 parent 67997f4 commit 81a9b9d

3 files changed

Lines changed: 25 additions & 24 deletions

File tree

src/community_db/templates/base.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ <h1>Welcome to the Pacific Connect Community Database</h1>
55
On this site, you can find details of members of the Pacific Connect Community Database
66
<br>
77
<form>
8-
{{ form.as_p }}
8+
<label for="id-search">Search:</label>
9+
<input type="text" name="search" id="id-search">
910
<input type="submit">
1011
</form>
1112
{% if search_text %}
Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
{% extends "base.html" %}
22

33
{% block content %}
4-
This is my list of folks
5-
<ul>
6-
{% for person in object_list %}
7-
<li>
8-
<a href="{% url 'fbv-person-detail' person.id %}">
9-
{{ person.first_name }} {{ person.last_name }}
10-
</a> from {{ person.country }}
11-
</li>
12-
{% endfor %}
13-
</ul>
14-
{% endblock %}
4+
{% if object_list %}
5+
This is my list of folks
6+
<ul>
7+
{% for person in object_list %}
8+
<li>
9+
<a href="{% url 'fbv-person-detail' person.id %}">
10+
{{ person.first_name }} {{ person.last_name }}
11+
</a> from {{ person.country }}
12+
</li>
13+
{% endfor %}
14+
</ul>
15+
{% else %}
16+
No profiles match that search
17+
{% endif %}
18+
{% endblock %}

src/community_db/views.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,21 @@
22
from django.shortcuts import render
33
from django.views.generic import DetailView, ListView
44

5-
from .forms import QuickSearchForm
65
from .models import Person
76

87
# FUNCTION BASED VIEWS
98

10-
# Searching the first name and last name fields using a Django form
9+
# Searching the first name and last name fields with text in the search box
1110
def list_persons_with_template(request):
12-
form = QuickSearchForm(request.GET)
11+
search_text = request.GET.get("search")
1312

1413
persons = Person.objects.all()
15-
search_text = ""
16-
if form.is_valid():
17-
search_text = form.cleaned_data["search"]
18-
if search_text:
19-
search_filters = models.Q(first_name__icontains=search_text) | models.Q(
20-
last_name__icontains=search_text
21-
)
22-
persons = persons.filter(search_filters)
23-
context = {"object_list": persons, "search_text": search_text, "form": form}
14+
if search_text:
15+
search_filters = models.Q(first_name__icontains=search_text) | models.Q(
16+
last_name__icontains=search_text
17+
)
18+
persons = persons.filter(search_filters)
19+
context = {"object_list": persons, "search_text": search_text}
2420
return render(request, "community_db/person_list_in_base.html", context)
2521

2622

0 commit comments

Comments
 (0)