Python Django Project – Library Book Finder
Get ready to crack interviews of top MNCs with Placement-ready courses Learn More!
A “Library Book Finder” typically refers to an online catalog or mobile app provided by libraries, enabling users to search for books and resources within their collections. It helps locate specific titles, check availability and may include additional features like placing holds or renewing items.
About the Python Library Book Finder Using Django
The Library Book Finder project aims to develop a comprehensive online platform that enables users to search for books, check availability, and access library resources. The project includes building a user-friendly interface, integrating with the library catalog database, and incorporating features such as book reservations, recommendations, and personalized user profiles.
Project Setup
Minimum system configuration:
- The operating system requirements include Windows 7 or later, macOS 10.11 or later, or a modern operating system.
- Linux distribution.
- Processor: Intel Core i3 or equivalent.
- RAM: 4 GB or more
- Disk Space: 5 GB or more.
- You have the flexibility to utilize browsers like Google Chrome, Mozilla Firefox, or Microsoft Edge for your browsing needs.
Visual Studio Code can be downloaded from the official website.
When accessing the download page, you have the option to choose the appropriate installer based on your operating system, be it Windows, macOS, or Linux. After downloading the installer, run it and proceed with the installation instructions to install VS Code on your computer.
Here’s a brief explanation of each step, along with the commands to execute:
1. To ensure Python is installed, please download and install the most recent version of Python from the official website. Follow the installation instructions provided for your specific operating system.
2. Install pip: Download the get-pip.py script and run python get-pip.py to install pip.
3. Create a virtual environment: Run python -m venv myenv to create a new virtual environment named ‘myenv’.
4. Activate the virtual environment: Run source myenv/bin/activate on Linux/Mac or myenv\Scripts\activate on Windows to activate the virtual environment.
5. Install Django: Run pip install django to install the latest stable version of Django.
6. Verify installation: Run python -m django –version to verify that Django is installed correctly.
7. Create a new Django project: Run the django-admin startproject project to create a new Django project named ‘project’.
8. Start the development server: Run python manage.py runserver to start the development server.
That’s it! A working installation of Django should now be in place, and you should be ready to start building your web application.
Download Python Django Library Book Finder Project
Please download the source code of Python Django Library Book Finder App Project: Python Django Library Book Finder Project Code.
Steps to Create Python Django Library Book Finder Project – Let’s Move ahead with amazing project.
1. Open the terminal from folder where we want to create the project.
Right-click on mouse -> open in terminal -> write “code .” (space is mandatory between code and “.”)
2. Then go to the project folder -> urls.py and inside urls.py of project, do this -> add “include” in import as shown in code below and add “path(“”, include(“app.urls”))”
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path("", include("app.urls"))
]
3. Create urls.py in app of library book finder project(urls.py is mandatory in both project and app).
4. In setting.py, add the ‘app name”. In my case, it is app only as below.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app',
]
5. Now runserver to check, if everything is working or not. If you see below image, then we are done with the installation part. To runserver, run command in terminal as follows “py manage.py runserver”.(if you see the rocket image, then it’s working fine).
6. views.py
from django.shortcuts import render, get_object_or_404
from .models import Book
from django.db.models import Q
# Create your views here.
def Index(request):
return render(request, "app/index.html")
def search_books(request):
query = request.GET.get('q')
results = []
if query:
# Search for books that match the query in title, description, or author
results = Book.objects.filter(
Q(title__icontains=query) |
Q(description__icontains=query) |
Q(author__icontains=query)
)
context = {
'query': query,
'results': results,
}
return render(request, 'app/index.html', context)
Urls.py
path("", Index, name="index"),
path('searchbook/', search_books, name='search_books'),
7. Home Page
templates/app -> index.html
{% load static %}
<!DOCTYPE html>
<html>
<head>
<title>Library Book Finder</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
}
.navbar {
background-color: #333;
color: #fff;
padding: 10px 0;
}
.container {
width: 80%;
margin: auto;
}
.search-container {
padding: 20px;
}
.search-bar {
padding: 10px;
width: 90%;
border-radius: 2px;
border: grey solid 1px;
}
.search-button {
padding: 10px;
background-color: #888;
color: #fff;
border: none;
cursor: pointer;
}
.book-container {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
padding: 20px;
}
.book {
width: 250px;
margin-bottom: 20px;
padding: 10px;
background-color: #fff;
border: 1px solid #ddd;
}
.search-results {
margin-top: 20px;
}
.cover {
width: 242px;
}
.poster img, h1 {
margin-left: 50%;
}
</style>
</head>
<body>
<div class="navbar">
<div class="container">
Library Book Finder
<a href="{% url 'favorite_list' %}" style="list-style: none;color: white; margin-left: 70%;">Favorites page</a>
</div>
</div>
<div class="container">
<div class="search-container">
<form method="GET" action="{% url 'search_books' %}">
<input type="text" class="search-bar" name="q" placeholder="Search for a book">
<button type="submit" class="search-button">Find a Book</button>
</form>
</div>
<div class="book-container">
<div class="search-results">
{% if results %}
{% for book in results %}
<div class="book">
<img class="cover" src="{{book.image.url}}">
<h3>{{ book.title }}</h3>
<p>Author: {{ book.author }}</p>
<p>Description: {{ book.description }}</p>
<p>Price: {{ book.Price }}</p>
<p>ISBN: {{ book.isbn }}</p>
<a href="{% url 'favourite' pk=book.pk %}"><input type="button" value="Add to favorite"></a>
</div>
{% endfor %}
{% else %}
<div class="poster">
<img src="{% static 'image/Twitter_Animated.gif' %}" alt="Book Poster">
<h1 style="color: grey;">PythonGeeks Library Book Finder </h1>
</div>
{% endif %}
</div>
</div>
</div>
</body>
</html>
Views.py
from django.shortcuts import render, redirect, get_object_or_404
from .models import Book
from django.db.models import Q
# Create your views here.
def Index(request):
return render(request, "app/index.html")
# Function to search for books based on user's query
def search_books(request):
query = request.GET.get('q') # Get the search query from GET parameters
results = [] # Initialize empty list to store search results
if query:
# Query the Book model to find books matching the search query
results = Book.objects.filter(
Q(title__icontains=query) |
Q(description__icontains=query) |
Q(author__icontains=query)
)
# Create a context dictionary for rendering the template
context = {
'query': query, # Store the search query
'results': results, # Store the search results
}
# Render the 'app/index.html' template with the context
return render(request, 'app/index.html', context)
# Function to add or remove a book from the user's favorite list
def favourite(request, pk):
book = get_object_or_404(Book, pk=pk)
# Get the book with the specified primary key
favorite_list = request.session.get('favorite_list', [])
# Get the current favorite list from session
# Check if the book is already in the favorite list
if book.pk in favorite_list:
# Remove the book from the favorite list
favorite_list.remove(book.pk)
Else:
# Add the book to the favorite list
favorite_list.append(book.pk)
# Update the user's session with the modified favorite list
request.session['favorite_list'] = favorite_list
# Redirect to the 'favourite' view
return redirect('favourite')
# Function to display the user's favorite book list
def favorite_list(request):
# Get the user's favorite list from session
favorite_list = request.session.get('favorite_list', []) favorite_books = Book.objects.filter(pk__in=favorite_list)
# Get favorite books using the Book model
# Render the 'app/favourite.html' template with the list of favorite books
return render(request, 'app/favourite.html', {'favorite_books': favorite_books})
Explained the above code in the form of comments.
8. templates/app -> favourites.html
<!DOCTYPE html>
<html>
<head>
<title>Favorite Books</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
}
.navbar {
background-color: #333;
color: #fff;
padding: 10px 0;
}
.container {
width: 80%;
margin: auto;
}
.book-container {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
padding: 20px;
}
.book {
width: 250px;
margin-bottom: 20px;
padding: 10px;
background-color: #fff;
border: 1px solid #ddd;
text-align: center;
}
.cover {
width: 100%;
height: auto;
}
.poster {
text-align: center;
margin-top: 50px;
}
</style>
</head>
<body>
<div class="navbar">
<div class="container">
Library Book Finder
</div>
</div>
<div class="container">
<h1>Favorite Books</h1>
<div class="book-container">
{% for book in favorite_books %}
<div class="book">
<img class="cover" src="{{ book.image.url }}" alt="{{ book.title }} Cover">
<h3>{{ book.title }}</h3>
<p>Author: {{ book.author }}</p>
<p>ISBN: {{ book.isbn }}</p>
<a href="{% url 'favourite' pk=book.pk %}"><input type="button" value="Remove from favorite"></a>
</div>
{% empty %}
<h4 style="color: grey;">No favorite book is added.</h4>
{% endfor %}
</div>
</div>
</body>
</html>
Urls.py
from django.urls import path, include
from .views import *
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path("", Index, name="index"),
path('searchbook/', search_books, name='search_books'),
path('favourite/<int:pk>/', favourite, name='favourite'),
path('favorite_list/',favorite_list, name='favorite_list')
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Admin Panel:
Now, how would we add data, delete, and update to the user side? Let’s come to the django inbuilt admin panel. So here are the details of the same:
1. Once you have your Django project set up and have created your models, you can use the admin panel to add data easily. Follow these steps:
2. Run python manage.py createsuperuser to create an admin user who can access the admin panel. Start the development server using python manage.py runserver. Open your browser and go to the admin panel URL (usually http://127.0.0.1:8000/admin/) Log in using the superuser credentials you created.
3. Then, Go to admin.py and add the following thing as follows:
from django.contrib import admin from .models import YourModelName admin.site.register(YourModelName)
Note: To display the model on the admin panel, you need to follow the third point.
Here are the images of the inbuilt admin
Panel of our project:
Note: You can clearly see the deletion and update from the in-built admin panel above.
Summary
A book library finder is a digital platform that helps users locate and access books across various libraries. It provides search functionality and detailed information about available books and enables users to check availability, reserve books, and discover nearby libraries, facilitating convenient access to a wide range of reading materials.





