Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

STEPS FOR Library Management System

USING DJANGO
1. Project creation command
• django-admin startproject park
2. App creation commands
• cd park
• python manage.py startapp Ammu
3. Development server running command
• python manage.py runserver
4. Open project directory and open the file named as “settings.py”
• inside that file add the following codes under the application definition and save
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'brmapp'
]

5. Open the project directory and open the file named “urls.py”
• Write the following code inside the file and save the file from django.contrib import admin
from django.contrib import admin
from django.urls import path,include

urlpatterns = [
path('admin/', admin.site.urls),
path("",include("brmapp.urls"))
]

6. Open the appname directory (ex: here folder named “home”)


• Create a python file named as “urls.py”.
• Write the following code inside that and save
from django.contrib import admin
from django.urls import path,include,re_path
from .views import helloView,addBookView,addBook,editBook,editBookView,deleteBookView

urlpatterns = [
path("",helloView),
path("add-book/",addBookView),
path("add-book/add",addBook),
path("edit-book/",editBookView),
path("edit-book/edit",editBook),
path("delete-book",deleteBookView)
]
7. Open the appname directory (ex: here folder named “home”)
• Open the python file named as “views.py”.
• Write the following code inside that from django.shortcuts import render from
from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect

from .models import Book


# Create your views here.
def helloView(request):
books=Book.objects.all()
return render(request,"viewbook.html",{"books":books})

def addBookView(request):
return render(request,"addbook.html")

def addBook(request):
if request.method=="POST":
t=request.POST["title"]
p=request.POST["price"]
print(t,p)
book=Book()
book.title=t
book.price=p
book.save()
return HttpResponseRedirect('/')

def editBook(request):
if request.method=="POST":
t=request.POST["title"]
p=request.POST["price"]

book=Book.objects.get(id=request.POST['bid'])
book.title=t
book.price=p
book.save()
return HttpResponseRedirect('/')

def editBookView(request):
book=Book.objects.get(id=request.GET['bookid'])
print(book)
return render(request,"edit-book.html",{"book":book})

def deleteBookView(request):
book=Book.objects.get(id=request.GET['bookid'])
book.delete()
return HttpResponseRedirect('/')
8. If we want to add more web pages inside our app, we have to create a new folder named as “templates”
under the project directory. • New pages shoulde created and placed inside the “templates” folder •
Necessary code should be added inside the file “views.py” of app folder

Templates app folder


1.Addbook.html

<!doctype html>
<html lang="en">
<head>
<!-- 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="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw
1T" crossorigin="anonymous">

<title>Add book | Home</title>


</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="/">Book Record Management</a>
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle
navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarText">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="/">Home <span class="sr-only">(current)</span></a>
</li>

<li class="nav-item active">


<a class="nav-link" href="add-book">add book </a>
</li>
</ul>
</div>
</nav>
<div class="container">
<h1 class="mt-2">Add Book</h1>

<form class="mt-4" action="/add-book/add" method="post">


{% csrf_token %}
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" id="title" aria-describedby="emailHelp"
placeholder="Enter Title" name="title">
<label for="price">Price</label>
<input type="text" class="form-control" id="price" aria-describedby="emailHelp"
placeholder="Enter Price" name="price">

</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>

<br>

<!-- Optional JavaScript -->


<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.14.7/dist/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHND
z0W1" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script>
</body>
</html>

2.Edit_book.html
<!doctype html>
<html lang="en">
<head>
<!-- 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="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw
1T" crossorigin="anonymous">

<title>Edit book | Home</title>


</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="/">Book Record Management</a>
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle
navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarText">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="/">Home <span class="sr-only">(current)</span></a>
</li>

<li class="nav-item active">


<a class="nav-link" href="/add-book">add book </a>
</li>
</ul>
</div>
</nav>

<div class="container">
<h1 class="mt-2">Edit Book</h1>

<form class="mt-4" action="/edit-book/edit" method="post">


{% csrf_token %}
<div class="form-group">
<input type="hidden" name="bid" value="{{book.id}}">
<label for="title">Title</label>
<input type="text" class="form-control" id="title" aria-describedby="emailHelp"
placeholder="Enter Title" name="title" value="{{book.title}}">
<label for="price">Price</label>
<input type="text" class="form-control" id="price" aria-describedby="emailHelp"
placeholder="Enter Price" name="price" value="{{book.price}}">

</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>

<br>

<!-- Optional JavaScript -->


<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.14.7/dist/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHND
z0W1" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script>
</body>
</html>

3.Viewbook.html

<!doctype html>
<html lang="en">
<head>
<!-- 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="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw
1T" crossorigin="anonymous">

<title>View book | Home</title>


</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="/">Book Record Management</a>
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle
navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarText">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="/">Home <span class="sr-only">(current)</span></a>
</li>

<li class="nav-item active">


<a class="nav-link" href="/add-book">add book </a>
</li>
</ul>
</div>
</nav>

<div class="container">

<h1 class="mt-2">All Books</h1>

<table class="table">
<thead>
<tr>
<th scope="col">index</th>
<th scope="col">Title</th>
<th scope="col">Price</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
{% for i in books %}
<tr>
<th scope="row">{{forloop.counter}}</th>
<td>{{i.title}}</td>
<td>{{i.price}}</td>
<td>

<a href="/edit-book?bookid={{i.id}}"><button type="button" class="btn


btn-primary">Edit</button></a>
<a href="/delete-book?bookid={{i.id}}"><button type="button" class="btn
btn-danger">Delete</button></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>

<!-- Optional JavaScript -->


<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.14.7/dist/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHND
z0W1" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script>
</body>
</html>
Output

You might also like