Professional Documents
Culture Documents
Untitled Document
Untitled Document
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"))
]
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
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
<!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">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<br>
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">
<div class="container">
<h1 class="mt-2">Edit Book</h1>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<br>
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">
<div class="container">
<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>