Skip to content

Commit

Permalink
Show message if no results
Browse files Browse the repository at this point in the history
  • Loading branch information
wsot committed Aug 22, 2022
1 parent 1e4bc9c commit 7d51d73
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
3 changes: 2 additions & 1 deletion src/community_db/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ <h1>Welcome to the Pacific Connect Community Database</h1>
On this site, you can find details of members of the Pacific Connect Community Database
<br>
<form>
{{ form.as_p }}
<label for="id-search">Search:</label>
<input type="text" name="search" id="id-search">
<input type="submit">
</form>
{% if search_text %}
Expand Down
26 changes: 15 additions & 11 deletions src/community_db/templates/community_db/person_list_in_base.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
{% extends "base.html" %}

{% block content %}
This is my list of folks
<ul>
{% for person in object_list %}
<li>
<a href="{% url 'fbv-person-detail' person.id %}">
{{ person.first_name }} {{ person.last_name }}
</a> from {{ person.country }}
</li>
{% endfor %}
</ul>
{% endblock %}
{% if object_list %}
This is my list of folks
<ul>
{% for person in object_list %}
<li>
<a href="{% url 'fbv-person-detail' person.id %}">
{{ person.first_name }} {{ person.last_name }}
</a> from {{ person.country }}
</li>
{% endfor %}
</ul>
{% else %}
No profiles match that search
{% endif %}
{% endblock %}
20 changes: 8 additions & 12 deletions src/community_db/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,21 @@
from django.shortcuts import render
from django.views.generic import DetailView, ListView

from .forms import QuickSearchForm
from .models import Person

# FUNCTION BASED VIEWS

# Searching the first name and last name fields using a Django form
# Searching the first name and last name fields with text in the search box
def list_persons_with_template(request):
form = QuickSearchForm(request.GET)
search_text = request.GET.get("search")

persons = Person.objects.all()
search_text = ""
if form.is_valid():
search_text = form.cleaned_data["search"]
if search_text:
search_filters = models.Q(first_name__icontains=search_text) | models.Q(
last_name__icontains=search_text
)
persons = persons.filter(search_filters)
context = {"object_list": persons, "search_text": search_text, "form": form}
if search_text:
search_filters = models.Q(first_name__icontains=search_text) | models.Q(
last_name__icontains=search_text
)
persons = persons.filter(search_filters)
context = {"object_list": persons, "search_text": search_text}
return render(request, "community_db/person_list_in_base.html", context)


Expand Down

0 comments on commit 7d51d73

Please sign in to comment.