add search functionality
parent
7c2350fd3e
commit
0275e13a96
|
@ -6,6 +6,12 @@ class NewBookForm(forms.Form):
|
||||||
bookid = forms.CharField(label='Book ID', max_length=12)
|
bookid = forms.CharField(label='Book ID', max_length=12)
|
||||||
def is_valid():
|
def is_valid():
|
||||||
return Book.objects.get(self.bookid) != None
|
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) != ''
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
{{ form }}
|
{{ form }}
|
||||||
<input type="submit" value="Add Book">
|
<input type="submit" value="Add Book">
|
||||||
</form>
|
</form>
|
||||||
|
<p>Or search for a book to add <a href="/bookshelves/{{bookshelfId}}/search">here</a></p>
|
||||||
|
|
||||||
<h1>Book List</h1>
|
<h1>Book List</h1>
|
||||||
{% if books %}
|
{% if books %}
|
||||||
|
|
|
@ -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">
|
<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 -->
|
<!-- Add additional CSS in static file -->
|
||||||
{% load static %}
|
{% load static %}
|
||||||
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
|
<link rel="stylesheet" href="{% static 'style.css' %}">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="container-fluid">
|
<div class="container-fluid">
|
||||||
|
|
|
@ -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 %}
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ from django.shortcuts import render, redirect
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
from .forms import NewBookForm
|
from .forms import NewBookForm, BookSearchForm
|
||||||
from django.views import generic
|
from django.views import generic
|
||||||
|
|
||||||
# class BookListView(generic.ListView):
|
# class BookListView(generic.ListView):
|
||||||
|
@ -52,6 +52,37 @@ def booksInBookshelf(request, bookshelfId):
|
||||||
|
|
||||||
return render(request, 'bookshelf.html', context=context)
|
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):
|
def bookshelfList(request):
|
||||||
bookshelves = Bookshelf.objects.all()
|
bookshelves = Bookshelf.objects.all()
|
||||||
|
|
|
@ -23,6 +23,7 @@ from django.conf.urls import include
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
path('bookshelves', views.bookshelfList, name='detail'),
|
path('bookshelves', views.bookshelfList, name='detail'),
|
||||||
|
path('bookshelves/<int:bookshelfId>/search', views.searchBooks, name='detail'),
|
||||||
path('bookshelves/<int:bookshelfId>', views.booksInBookshelf, name='detail')
|
path('bookshelves/<int:bookshelfId>', views.booksInBookshelf, name='detail')
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue