0% found this document useful (0 votes)
74 views

Django Project Structure 2

The document describes creating a Django web application that allows users to view, add, update and delete student records using class-based views. It includes models, views and templates to support CRUD operations on a Student model.

Uploaded by

Mahesh Kumar
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)
74 views

Django Project Structure 2

The document describes creating a Django web application that allows users to view, add, update and delete student records using class-based views. It includes models, views and templates to support CRUD operations on a Student model.

Uploaded by

Mahesh Kumar
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
You are on page 1/ 6

Create a small project that will capture student details and will allow the user

to Insert, Update, Delete and View all the student details. Use Class
Based Views for the same.
Solution:
Models.py
from django.db import models

class Student(models.Model):
name = models.CharField(max_length=100)
roll_number = models.CharField(max_length=10)
age = models.IntegerField()

def __str__(self):
return self.name

views.py
from django.urls import reverse_lazy
from django.views.generic import ListView, DetailView, CreateView, UpdateView,
DeleteView
from .models import Student

class StudentListView(ListView):
model = Student
template_name = 'student/student_list.html'
context_object_name = 'students'

class StudentDetailView(DetailView):
model = Student
template_name = 'student/student_detail.html'
context_object_name = 'student'

class StudentCreateView(CreateView):
model = Student
template_name = 'student/student_form.html'
fields = ['name', 'roll_number', 'age']

class StudentUpdateView(UpdateView):
model = Student
template_name = 'student/student_form.html'
fields = ['name', 'roll_number', 'age']
class StudentDeleteView(DeleteView):
model = Student
template_name = 'student/student_confirm_delete.html'
success_url = reverse_lazy('student_list')

Templates:

(i) Student_list.html
<!DOCTYPE html>
<html>
<head>
<title>Student List</title>
</head>
<body>
<h1>Student List</h1>
<ul>
{% for student in students %}
<li><a href="{% url 'student_detail' student.pk %}">{{ student.name
}}</a></li>
{% endfor %}
</ul>
<a href="{% url 'student_create' %}">Add New Student</a>
</body>
</html>

(ii)student_datail.html
<!DOCTYPE html>
<html>
<head>
<title>{{ student.name }} Details</title>
</head>
<body>
<h1>{{ student.name }} Details</h1>
<p>Name: {{ student.name }}</p>
<p>Roll Number: {{ student.roll_number }}</p>
<p>Age: {{ student.age }}</p>
<a href="{% url 'student_edit' student.pk %}">Edit</a>
<a href="{% url 'student_delete' student.pk %}">Delete</a>
<a href="{% url 'student_list' %}">Back to Student List</a>
</body>
</html>

(iii)Student_form.html
<!DOCTYPE html>
<html>
<head>
<title>{% if form.instance.pk %}Edit{% else %}Create{% endif %}
Student</title>
</head>
<body>
<h1>{% if form.instance.pk %}Edit{% else %}Create{% endif %} Student</h1>
<form method="post" action="{% if form.instance.pk %}{% url 'student_edit'
form.instance.pk %}{% else %}{% url 'student_create' %}{% endif %}">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save</button>
</form>
<a href="{% url 'student_list' %}">Cancel</a>
</body>
</html>

(iv)Student_confirm_delete.html
<!-- student/student_confirm_delete.html -->
<!DOCTYPE html>
<html>
<head>
<title>Confirm Deletion</title>
</head>
<body>
<h1>Confirm Deletion</h1>
<p>Are you sure you want to delete {{ student.name }}?</p>
<form method="post" action="{% url 'student_delete' student.pk %}">
{% csrf_token %}
<button type="submit">Confirm</button>
</form>
<a href="{% url 'student_list' %}">Cancel</a>
</body>
</html>

Student/urls.py
from django.urls import path
from .views import StudentListView, StudentDetailView, StudentCreateView,
StudentUpdateView, StudentDeleteView

urlpatterns = [
path('', StudentListView.as_view(), name='student_list'),
path('<int:pk>/', StudentDetailView.as_view(), name='student_detail'),
path('new/', StudentCreateView.as_view(), name='student_create'),
path('<int:pk>/edit/', StudentUpdateView.as_view(), name='student_edit'),
path('<int:pk>/delete/', StudentDeleteView.as_view(), name='student_delete'),
]

Student_project/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('students/', include('student.urls')),
]

Output:

You might also like