add search functionality

adam
aundus 2021-02-11 20:05:08 -05:00
parent 7c2350fd3e
commit 0275e13a96
6 changed files with 72 additions and 2 deletions

View File

@ -8,6 +8,12 @@ class NewBookForm(forms.Form):
return Book.objects.get(self.bookid) != None
class BookSearchForm(forms.Form):
searchTerm = forms.CharField(label='Search Term', max_length=64)
def is_valid():
return str(searchTerm) != ''

View File

@ -12,6 +12,7 @@
{{ form }}
<input type="submit" value="Add Book">
</form>
<p>Or search for a book to add <a href="/bookshelves/{{bookshelfId}}/search">here</a></p>
<h1>Book List</h1>
{% if books %}

View File

@ -7,7 +7,7 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<!-- Add additional CSS in static file -->
{% load static %}
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
<link rel="stylesheet" href="{% static 'style.css' %}">
</head>
<body>
<div class="container-fluid">

View File

@ -0,0 +1,31 @@
{% extends "generic_template.html" %}
{% block content %}
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<h1>Search Books</h1>
<strong>Number of Books found:</strong> <p>{{ total }}</p>
<form action="/bookshelves/{{bookshelfId}}/search" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Search">
</form>
<h1>Book List</h1>
{% if books %}
<ul>
{% for book in books %}
<li class="bookshelf-entry" id="book-{{ book.id }}">
<a href="https://www.gutenberg.org/ebooks/{{ book.id }}">{{ book.title }}</a>
</li>
{% endfor %}
</ul>
{% else %}
<p>There are no books found.</p>
{% endif %}
{% load static %}
<!-- <script src="{% static 'booklist.js' %}"></script> -->
<link rel="stylesheet" href="{% static 'style.css' %}"></script>
{% endblock %}

View File

@ -4,7 +4,7 @@ from django.shortcuts import render, redirect
from .forms import NewBookForm
from .forms import NewBookForm, BookSearchForm
from django.views import generic
# class BookListView(generic.ListView):
@ -52,6 +52,37 @@ def booksInBookshelf(request, bookshelfId):
return render(request, 'bookshelf.html', context=context)
def searchBooks(request, bookshelfId):
context = {
'books': None,
'total': 0,
'bookshelfId': bookshelfId
}
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = (request.POST)
# print(form)
# check whether it's valid:
if form.get('searchTerm') != None:
foundBooks = getBooksMatchingTitle(form.get('searchTerm'))
context['books'] = foundBooks
context['total'] = len(foundBooks)
context['form'] = BookSearchForm()
# redirect to a new URL:
return render(request, 'search.html', context=context)
# if a GET (or any other method) we'll create a blank form
else:
form = BookSearchForm()
context['form'] = form
return render(request, 'search.html', context=context)
def getBooksMatchingTitle(term):
return Book.objects.filter(title__contains=term)
def bookshelfList(request):
bookshelves = Bookshelf.objects.all()

View File

@ -23,6 +23,7 @@ from django.conf.urls import include
urlpatterns = [
path('admin/', admin.site.urls),
path('bookshelves', views.bookshelfList, name='detail'),
path('bookshelves/<int:bookshelfId>/search', views.searchBooks, name='detail'),
path('bookshelves/<int:bookshelfId>', views.booksInBookshelf, name='detail')
]