DEV Community

Ermelinda Rapoli
Ermelinda Rapoli

Posted on • Updated on

Library web application with Django Framework

A web applicaton using Django Framework to manage a library

My goal with this project is to show the different aspects of django framework for models, urls, template, view, settings, using of packages, admin control panel.
I will show this creating a web application to manage a library database. Here a brief description:
• The project consists in reproducing a part of an application for the management of the database of a library
• There are three entities: book, author and genre
• Genre attributes: name
• Author attributes: name, surname, country, photo author
• Book attributes: title, author, genre, isbn, image for the cover
• Each Book can belong to multiple genres (many-to-many relationship)
• A book is associated with only one author
• It is possible to view a list of books in the library from the website homepage; all the details are shown for each book
• It is possible to access a "author profile" page which shows all the books present for that author, and his country of origin
• Books, Genres and Authors are added from the Administration Panel


Having installed python on your system, the version used for this project is python 3.7.7
The operating system on which the project was run is Windows 10.
Django Framework version: 3.0.5

1. I create a virtual development environment (which I call library_env) for this project:

  • a. I create a virtual environment

python -m venv library_env

  • b. The virtual environment is active

library_env / Scripts / activate.bat

  • c. Django installation

pip install Django

  • d. I create a directory for the project

mkdir library-project

2. I create a Django project which I call libraryproject:

  • a. In the library-project folder I run the command

django-admin startproject libraryproject

  • b. The previous command creates a project called libraryproject and inside it another folder called libraryproject, which turns out to be the main app of the whole project. The folder tree will look like this:

  • c. Check that everything is working by starting the web server made available by Django

    python runserver

  • d. From browser, navigate to URL and the default homepage of the project will be shown

3. I create the library application:

  • a. Inside the path library-project \ libraryproject I run the command

python startapp library

  • b. The tree looks like this:

  • c. I register the app library in the of the app libraryproject

4. Creation of models

I edit the library \ file:

  • a. Genre Model:

  • b. Author Model - since the Author model has the country field among the attributes, I decided to import a python package that would already make the list of countries available, in order to make it easier to create an author from the administrator panel (via selection)

-- i. I import the package using the command

pip install django-countries

-- ii. I add django_countries in INSTALLED_APPS in the file

-- iii. For the Author model, the reverse method must be imported:
from django.urls import reverse. This is he Author model

c. Book template. For this model I imported the pillow package to have available the type of ImageField field to use for the cover field

pip install pillow

This is the Book model:

To allow the image files to be uploaded, I modify the project file by inserting, just under the definition of BASE_DIR

MEDIA_URL = "/media/"
MEDIA_ROOT=os.path.join(BASE_DIR, 'media')

And also edit the file of the project by inserting:

from django.conf import settings
from django.conf.urls.static import static # new
if settings.DEBUG: # new
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

5. We migrate the newly created models

  • a. I run the python command

python makemigrations

From the command line I have this result:

Migrations for 'library':
    - Create model Author
    - Create model Category
    - Create model Book
  • b. I run from the command line python sqlmigrate library 0001 and I get the result of executing the sql commands for the creation of the tables with the related fields
  • c. I make sql commands effective, by running them on the database using the command python migrate

6. I create a superuser account to access the django Admin panel:

  • a. I run the python command

    python createsuperuser

  • b. Enter a username when prompted

  • c. An email when requested (optional)

  • d. Password and password confirmation when requested

  • e. Start the django web server using the python command

    python runserver

  • f. Browse to the URL

  • g. Enter the username and password of the superuser created in step a.

  • h. The administrator panel will be shown like this:

That is, I have the management of groups and users, which are created by default by django, let's see how to make the resources of our app available.

7. I make the CRUD of the models available directly from the Admin section:

  • a. I register the models in the library\ file

  • b. In the administration panel I now manage the resources for the app Library:

From this administrator panel I can add new genres, books and authors, edit and delete.

8. Implementation of the "homepage" request or navigation to the URL

a. I create the file in the app's library folder
b. I edit the file libraryproject \

  • c. In the library \ file I write this:

That is, I am saying that the url "" is managed by the view home that we are now going to define

  • d. I define the view home: the. I modify the library \ file going to define the home method in this way:

So I added the home view which renders the home page via the homepage.html template, which I am now going to create

  • e. I create the template homepage.html:

-- i. I take advantage of the inheritance between the templates. I therefore define a base.html template where I define the skeleton of an html page that will be inherited from all the other templates. All other templates will only load the necessary content. Also I want to give the pages of my site some bootstrap-style css

-- ii. I create the templates directory within the library-project \ libraryproject \ templates project

-- iii. I create the base.html file in the templates folder. The base.html will look like this:

<!doctype html>
<html lang="en">
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

    <title>{% block head_title %}Library | {% endblock head_title %}</title>

    <div class="container">
      <h1>Library Store</h1>

        {% block content %}

        {% endblock content %}


    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

-- iv. I create homepage.html in the templates folder. The template shows the title of the current page in the head_title block, the list of all books with details in the content block and the list of all authors, each one will be a link to the info details page for that author. The template will look like this:

{%  extends 'base.html' %}
{% block head_title %}
    {{ block.super }}
{% endblock head_title %}
{% block content %}
    <div class="row">
        {% for book in books %}
        <div class="col-6">
          <div class="card mb-3" style="max-width: 540px;">
            <div class="row no-gutters">
              <div class="col-md-4">
                <img src="{{ book.cover.url }}" class="card-img" alt="...">
              <div class="col-md-8">
                <div class="card-body">
                  <h5 class="card-title">{{ book.title }}</h5>
                  <p class="card-text">ISBN: {{ book.isbn }}</p>
                  <p class="card-text">Author: <a href="{{ }}">{{ }}</a></p>
                  <p class="card-text">
                      <small class="text-muted">
                          Categories: {{ book.categories.all|join:", " }}
    {% endfor %}

    <h4>Authors list</h4>
    {%for author in authors %}
        <li><a href="{{ author.get_absolute_url }}">{{ author }}</a></li>
    {% endfor %}
{% endblock content %}
  • f. Start the django web server python runserver and browse from browser to url to see the result

9. Implementation of the author profile page:

  • a. I edit the of the app library and add a new path
path ("author / <int: pk>", AuthorDetailViewCB.as_view (), name = "author_detail"),

by importing the AuthorDetailViewCB view that I now create

  • b. I create the AuthorDetailViewCB view in the library\ file:

The view in question extends Django's DetailView class, where I am telling the view to respond to the "author / pk" request by rendering the author_detail.html template, where pk is the primary key of the author resource (ie the id) and to use the Author model.

  • c. I create the templates directory in the library folder
  • d. I create the author_detail.html file under the library\templates folder
{%  extends 'base.html' %}
{% block head_title %}
    {{ block.super }}
    Author {{ }}
{% endblock head_title %}
{% block content %}
        <h4>Author Info:</h4>
          <div class="card mb-3" style="max-width: 540px;">
            <div class="row no-gutters">
              <div class="col-md-4">
                <img src="{{ }}" class="card-img" alt="...">
              <div class="col-md-8">
                <div class="card-body">
                  <h5 class="card-title">Name: {{ }}</h5>
                  <p class="card-text">Surname: {{ author.surname }}</p>
                  <p class="card-text">State: {{ }}</p>
                  <p class="card-text">
                      Libri scritti:
                      {{ author.books.all|join:", "}}
      <a href="{% url 'library-home' %}">Return To Home</a>
{% endblock content %}
  • e. I modify the file of the project in order to set the possibility of using the templates folder that I created in the library directory. I edit the settings file by going to set DIRS like this:
'DIRS': [os.path.join(BASE_DIR, 'templates')],
  • f. Start the django web server python runserver and browse from browser to url to see the result

You can find all project source code on my


Top comments (1)

taufiqnet profile image
Md. Taufiqur Rahman

It was a great start. Then what about the full application. Is there any update about the features?