0% found this document useful (0 votes)
8 views17 pages

Unit 4

The document outlines three experiments in Django: creating an app to save and fetch student data, performing CRUD operations on student records, and implementing session management and cookies. Each experiment includes steps for setting up the project, creating models, views, and templates, as well as running the server. The document provides detailed code snippets and instructions for each step involved in the experiments.

Uploaded by

satya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views17 pages

Unit 4

The document outlines three experiments in Django: creating an app to save and fetch student data, performing CRUD operations on student records, and implementing session management and cookies. Each experiment includes steps for setting up the project, creating models, views, and templates, as well as running the server. The document provides detailed code snippets and instructions for each step involved in the experiments.

Uploaded by

satya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIT - 4

EXPERIMENT–1
Create an app in Django which saves data into database and fetches data and shows as list
E:\unit4\exp1>python -m venv venv
E:\unit4\exp1>venv\Scripts\activate
(env) E:\unit4\exp1>pip install django

STEP 1: Create Project


django-admin startproject db_list_project
cd db_list_project

STEP 2: Create App


python [Link] startapp studentapp

STEP 3: Add App in [Link]


db_list_project/[Link]

INSTALLED_APPS = [

'studentapp',
]

STEP 4: Create Model (Database Table)


studentapp/[Link]

from [Link] import models


class Student([Link]):
name = [Link](max_length=30)
marks = [Link]()
def __str__(self):
return [Link]
STEP 5: Migrate Database
python [Link] makemigrations
python [Link] migrate

#Note: Data stored in: db_list_project/db.sqlite3

🔹 STEP 6: View (Save + Fetch)


studentapp/[Link]

from [Link] import render


from .models import Student
def student_list(request):
if [Link] == 'POST':
name = [Link]['name']
marks = [Link]['marks']
[Link](name=name, marks=marks)
students = [Link]()
return render(request, '[Link]', {'students': students})

STEP 7: URLs (App)


studentapp/[Link]

from [Link] import path


from .views import student_list

urlpatterns = [
path('', student_list),
]

STEP 8: URLs (Project)


db_list_project/[Link]
from [Link] import admin
from [Link] import path , include

urlpatterns = [
path('admin/', [Link]),
path('', include('[Link]')),
]

STEP 9: Template
Create folder: studentapp/templates/
[Link]

<!DOCTYPE html>
<html>
<body>

<h2>Add Student</h2>
<form method="post">
{% csrf_token %}
Name: <input type="text" name="name"><br><br>
Marks: <input type="number" name="marks"><br><br>
<button>Save</button>
</form>
<hr>
<h2>Student List</h2>
<ul>
{% for s in students %}
<li>{{ [Link] }} - {{ [Link] }}</li>
{% endfor %}
</ul>
</body>
</html>

STEP 10: Run Server


python [Link] runserver
Open:
[Link]

✅ OUTPUT
EXPERIMENT–2
Create an app in Django for performing CRUD operations on records in a database
CRUD = Create, Read, Update, Delete

E:\unit4\exp2>python -m venv venv


E:\unit4\exp2>venv\Scripts\activate
(env) E:\unit4\exp2>pip install django

STEP 1: Create Project


django-admin startproject crud_project
cd crud_project

STEP 2: Create App


python [Link] startapp studentapp

STEP 3: Add App in [Link]


crud_project/[Link]

INSTALLED_APPS = [

'studentapp',
]

STEP 4: Create Model


studentapp/[Link]

from [Link] import models


class Student([Link]):
name = [Link](max_length=30)
marks = [Link]()
def __str__(self):
return [Link]

STEP 5: Migrate Database


python [Link] makemigrations
python [Link] migrate

#Note: Database file: crud_project/db.sqlite3

STEP 6: Views (CRUD Logic)


studentapp/[Link]

from [Link] import render, redirect


from .models import Student

def student_list(request):
students = [Link]()
return render(request, '[Link]', {'students': students})

def add_student(request):
if [Link] == 'POST':
[Link](
name=[Link]['name'],
marks=[Link]['marks']
)
return redirect('/')
return render(request, '[Link]')

def update_student(request, id):


student = [Link](id=id)
if [Link] == 'POST':
[Link] = [Link]['name']
[Link] = [Link]['marks']
[Link]()
return redirect('/')
return render(request, '[Link]', {'student': student})

def delete_student(request, id):


student = [Link](id=id)
[Link]()
return redirect('/')

STEP 7: URLs (App)


studentapp/[Link]

from [Link] import path


from .views import *

urlpatterns = [
path('', student_list),
path('add/', add_student),
path('update/<int:id>/', update_student),
path('delete/<int:id>/', delete_student),
]

STEP 8: URLs (Project)


crud_project/[Link]

from [Link] import admin


from [Link] import path , include

urlpatterns = [
path('admin/', [Link]),
path('', include('[Link]')),
]

STEP 9: Templates
Create folder: studentapp/templates/

[Link] (READ)

<!DOCTYPE html>
<html>
<body>

<h2>Student List</h2>

<a href="/add/">Add Student</a>

<table border="1">
<tr>
<th>Name</th>
<th>Marks</th>
<th>Actions</th>
</tr>

{% for s in students %}
<tr>
<td>{{ [Link] }}</td>
<td>{{ [Link] }}</td>
<td>
<a href="/update/{{ [Link] }}/">Edit</a>
<a href="/delete/{{ [Link] }}/">Delete</a>
</td>
</tr>
{% endfor %}

</table>
</body>
</html>

[Link] (CREATE)

<!DOCTYPE html>
<html>
<body>

<h2>Add Student</h2>

<form method="post">
{% csrf_token %}
Name: <input type="text" name="name"><br><br>
Marks: <input type="number" name="marks"><br><br>
<button>Save</button>
</form>

</body>
</html>

[Link] (UPDATE)

<!DOCTYPE html>
<html>
<body>
<h2>Update Student</h2>

<form method="post">
{% csrf_token %}
Name: <input type="text" name="name" value="{{ [Link] }}"><br><br>
Marks: <input type="number" name="marks" value="{{ [Link] }}"><br><br>
<button>Update</button>
</form>

</body>
</html>

STEP 10: Run Server


python [Link] runserver
Open: [Link]
EXPERIMENT–3
Create an app in Django which uses Session Management and Cookies

WHAT THIS PROGRAM SHOWS


✔ Create session
✔ Read session
✔ Delete session
✔ Create cookie
✔ Read cookie

E:\unit4\exp3>python -m venv venv


E:\unit4\exp3>venv\Scripts\activate
(env) E:\unit4\exp3>pip install django

STEP 1: Create Project


django-admin startproject session_cookie_project
cd session_cookie_project

STEP 2: Create App


python [Link] startapp demoapp

STEP 3: Add App in [Link]


session_cookie_project/[Link]

INSTALLED_APPS = [

'demoapp',
]
STEP 4: Views (Session & Cookie)
demoapp/[Link]

from [Link] import HttpResponse

# SESSION
def set_session(request):
[Link]['username'] = 'student'
return HttpResponse("Session is set")

def get_session(request):
name = [Link]('username', 'No Session')
return HttpResponse("Session value: " + name)

def delete_session(request):
[Link]()
return HttpResponse("Session deleted")

# COOKIE
def set_cookie(request):
response = HttpResponse("Cookie is set")
response.set_cookie('course', 'Django')
return response

def get_cookie(request):
course = [Link]('course', 'No Cookie')
return HttpResponse("Cookie value: " + course)
STEP 5: URLs (App)
demoapp/[Link]

from [Link] import path


from .views import *

urlpatterns = [
path('set-session/', set_session),
path('get-session/', get_session),
path('delete-session/', delete_session),

path('set-cookie/', set_cookie),
path('get-cookie/', get_cookie),
]

STEP 6: URLs (Project)


session_cookie_project/[Link]

from [Link] import admin


from [Link] import path , include

urlpatterns = [
path('admin/', [Link]),
path('', include('[Link]')),
]

STEP 7: Run Server


python [Link] migrate
python [Link] runserver
STEP 8: TEST IN BROWSER (IMPORTANT)
Session
[Link]
[Link]
[Link]

Cookie
[Link]
[Link]

You might also like