django student project
Django Student CRUD Application (Complete Reference)
This guide walks through building a simple Django Student Management application with CRUD (Create, Read, Update, Delete) functionality.
Step 1: Create Project
django-admin startproject school
Go inside the project.
cd school
Step 2: Create App
python manage.py startapp student
Project structure becomes:
school/
│
├── manage.py
├── school/
│ ├── settings.py
│ ├── urls.py
│ └── ...
│
└── student/
├── admin.py
├── apps.py
├── forms.py
├── migrations/
├── models.py
├── templates/
│ └── student/
├── urls.py
└── views.py
Step 3: Register the App
Open school/settings.py
INSTALLED_APPS = [
...
"student",
]
Step 4: Configure Templates
If using only app templates:
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
"django.template.context_processors.debug",
],
},
},
]
Recommended project structure:
school/
│
├── templates/
│ └── base.html
│
├── student/
│ └── templates/
│ └── student/
│ ├── student_list.html
│ ├── student_form.html
│ ├── student_detail.html
│ └── student_confirm_delete.html
Then configure
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [BASE_DIR / "templates"],
"APP_DIRS": True,
},
]
Step 5: Create Model
student/models.py
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
school = models.CharField(max_length=200)
location = models.CharField(max_length=200)
contact = models.CharField(max_length=20)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ["-created_at"]
def __str__(self):
return self.name
Explanation
- CharField stores text.
- auto_now_add stores creation time.
- ordering displays latest record first.
- __str__ makes the object readable in Django Admin.
Step 6: Register Model in Admin
student/admin.py
from django.contrib import admin
from .models import Student
@admin.register(Student)
class StudentAdmin(admin.ModelAdmin):
list_display = (
"name",
"school",
"location",
"contact",
"created_at",
)
search_fields = (
"name",
"school",
"location",
"contact",
)
list_filter = (
"school",
"location",
)
ordering = ("-created_at",)
Explanation
- list_display → columns shown in admin.
- search_fields → enables searching.
- list_filter → sidebar filters.
- ordering → latest first.
Step 7: Create ModelForm
student/forms.py
from django import forms
from .models import Student
class StudentForm(forms.ModelForm):
class Meta:
model = Student
fields = [
"name",
"school",
"location",
"contact",
]
Explanation
Instead of manually creating HTML inputs and validation, ModelForm automatically generates them using the model.
Step 8: Create Views
student/views.py
from django.shortcuts import render, redirect, get_object_or_404
from .forms import StudentForm
from .models import Student
def student_list(request):
students = Student.objects.all()
return render(
request,
"student/student_list.html",
{"students": students},
)
def student_detail(request, pk):
student = get_object_or_404(Student, pk=pk)
return render(
request,
"student/student_detail.html",
{"student": student},
)
def student_create(request):
if request.method == "POST":
form = StudentForm(request.POST)
if form.is_valid():
form.save()
return redirect("student_list")
else:
form = StudentForm()
return render(
request,
"student/student_form.html",
{"form": form},
)
def student_update(request, pk):
student = get_object_or_404(Student, pk=pk)
if request.method == "POST":
form = StudentForm(
request.POST,
instance=student,
)
if form.is_valid():
form.save()
return redirect("student_list")
else:
form = StudentForm(instance=student)
return render(
request,
"student/student_form.html",
{"form": form},
)
def student_delete(request, pk):
student = get_object_or_404(Student, pk=pk)
if request.method == "POST":
student.delete()
return redirect("student_list")
return render(
request,
"student/student_confirm_delete.html",
{"student": student},
)
Explanation
student_list()
Shows every student.
student_detail()
Shows one student's information.
student_create()
Creates a new student.
student_update()
Edits an existing student.
student_delete()
Deletes a student after confirmation.
Step 9: App URLs
student/urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.student_list, name="student_list"),
path("create/", views.student_create, name="student_create"),
path("<int:pk>/", views.student_detail, name="student_detail"),
path("<int:pk>/edit/", views.student_update, name="student_update"),
path("<int:pk>/delete/", views.student_delete, name="student_delete"),
]
Explanation
students/
students/create/
students/1/
students/1/edit/
students/1/delete/
Step 10: Project URLs
school/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path("admin/", admin.site.urls),
path("students/", include("student.urls")),
]
Explanation
This connects your student app to the main project.
Step 11: Templates
base.html
<!DOCTYPE html>
<html>
<head>
<title>Student Management</title>
</head>
<body>
<h1>Student Management</h1>
<hr>
<a href="{% url 'student_list' %}">Home</a>
|
<a href="{% url 'student_create' %}">Add Student</a>
<hr>
{% block content %}
{% endblock %}
</body>
</html>
student_list.html
{% extends "base.html" %}
{% block content %}
<h2>Students</h2>
<table border="1">
<tr>
<th>Name</th>
<th>School</th>
<th>Location</th>
<th>Contact</th>
<th>Action</th>
</tr>
{% for student in students %}
<tr>
<td>{{ student.name }}</td>
<td>{{ student.school }}</td>
<td>{{ student.location }}</td>
<td>{{ student.contact }}</td>
<td>
<a href="{% url 'student_detail' student.id %}">View</a>
|
<a href="{% url 'student_update' student.id %}">Edit</a>
|
<a href="{% url 'student_delete' student.id %}">Delete</a>
</td>
</tr>
{% empty %}
<tr>
<td colspan="5">No students found.</td>
</tr>
{% endfor %}
</table>
{% endblock %}
student_form.html
{% extends "base.html" %}
{% block content %}
<h2>Student Form</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">
Save
</button>
</form>
{% endblock %}
student_detail.html
{% extends "base.html" %}
{% block content %}
<h2>Student Details</h2>
<p>Name : {{ student.name }}</p>
<p>School : {{ student.school }}</p>
<p>Location : {{ student.location }}</p>
<p>Contact : {{ student.contact }}</p>
<a href="{% url 'student_update' student.id %}">
Edit
</a>
{% endblock %}
student_confirm_delete.html
{% extends "base.html" %}
{% block content %}
<h2>Delete Student</h2>
<p>
Are you sure you want to delete
<strong>{{ student.name }}</strong> ?
</p>
<form method="post">
{% csrf_token %}
<button type="submit">
Yes Delete
</button>
</form>
{% endblock %}
Step 12: Create Database
python manage.py makemigrations
Explanation
Creates migration files from your models.
python manage.py migrate
Explanation
Creates database tables.
Step 13: Create Superuser
python manage.py createsuperuser
Follow the prompts for:
- Username
- Password
Step 14: Run the Server
python manage.py runserver
Open
http://127.0.0.1:8000/students/
Admin
http://127.0.0.1:8000/admin/
CRUD Flow
Create
↓
Stored in Database
↓
List View
↓
Detail View
↓
Update
↓
Delete
Common Errors
ImportError: cannot import StudentForm
Cause
forms.py not saved
or
StudentForm missing
NoReverseMatch
Usually caused by incorrect URL names.
Check
student_list
student_create
student_update
student_delete
student_detail
TemplateDoesNotExist
Check
student/
templates/
student/
or configure
"DIRS": [BASE_DIR / "templates"]
No module named student
Forgot to add
"student"
to
INSTALLED_APPS
What You Learned
- Creating a Django project and app
- Defining models
- Registering models in the admin
- Using
ModelFormfor form generation and validation - Writing function-based CRUD views
- Configuring app and project URLs
- Organizing templates
- Running migrations
- Using the Django admin interface
- Common troubleshooting tips
This project provides a solid foundation for learning Django CRUD operations and can be extended with features like Bootstrap styling, authentication, search, pagination, and class-based views.