在这里,我将为您提供一个简单的 Django 学生管理系统的源代码实现,包括学生的增删改查功能。首先,请确保您已经安装了 Django,并创建了一个 Django 项目。在本示例中,我们将创建一个名为"student_management"的 Django 应用程序。
打开终端或命令行窗口,并运行以下命令:
django-admin startproject student_project
cd student_project
python manage.py startapp student_management
打开 student_management/models.py
文件,并添加以下学生模型:
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
roll_number = models.CharField(max_length=20)
email = models.EmailField()
def __str__(self):
return self.name
在终端中运行以下命令,创建数据库表格:
python manage.py makemigrations
python manage.py migrate
打开 student_management/views.py
文件,并添加以下视图函数:
from django.shortcuts import render, get_object_or_404, redirect
from .models import Student
from .forms import StudentForm
def student_list(request):
students = Student.objects.all()
return render(request, 'student_list.html', {'students': students})
def student_detail(request, pk):
student = get_object_or_404(Student, pk=pk)
return render(request, '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_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_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_confirm_delete.html', {'student': student})
在 student_management/forms.py
文件中创建学生表单:
from django import forms
from .models import Student
class StudentForm(forms.ModelForm):
class Meta:
model = Student
fields = ['name', 'age', 'roll_number', 'email']
在 student_management/templates
目录下创建以下 HTML 模板文件:
student_list.html
:
<!DOCTYPE html>
<html>
<head>
<title>Student List</title>
</head>
<body>
<h1>Student List</h1>
{% for student in students %}* <a href="{% url 'student_detail' student.pk %}">{{ student.name }}</a>
<a href="{% url 'student_create' %}">Add Student</a>
</body>
</html>
student_detail.html
:
<!DOCTYPE html>
<html>
<head>
<title>Student Detail</title>
</head>
<body>
<h1>Student Detail</h1>
<p>Name: {{ student.name }}</p>
<p>Age: {{ student.age }}</p>
<p>Roll Number: {{ student.roll_number }}</p>
<p>Email: {{ student.email }}</p>
<a href="{% url 'student_list' %}">Back to List</a>
<a href="{% url 'student_update' student.pk %}">Edit</a>
<a href="{% url 'student_delete' student.pk %}">Delete</a>
</body>
</html>
student_form.html
:
<!DOCTYPE html>
<html>
<head>
<title>Student Form</title>
</head>
<body>
<h1>Student Form</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save">
</form>
<a href="{% url 'student_list' %}">Cancel</a>
</body>
</html>
student_confirm_delete.html
:
<!DOCTYPE html>
<html>
<head>
<title>Confirm Delete</title>
</head>
<body>
<h1>Confirm Delete</h1>
<p>Are you sure you want to delete "{{ student.name }}"?</p>
<form method="post">
{% csrf_token %}
<input type="submit" value="Confirm">
</form>
<a href="{% url 'student_list' %}">Cancel</a>
</body>
</html>
打开 student_management/urls.py
文件,并添加以下 URL 模式:
from django.urls import path
from . import views
urlpatterns = [
path('', views.student_list, name='student_list'),
path('student/<int:pk>/', views.student_detail, name='student_detail'),
path('student/create/', views.student_create, name='student_create'),
path('student/<int:pk>/update/', views.student_update, name='student_update'),
path('student/<int:pk>/delete/', views.student_delete, name='student_delete'),
]
打开 student_project/urls.py
文件,并将应用程序的 URL 模式包含进去:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('student_management.urls')),
]
现在,您已经完成了 Django 学生增删改查功能的实现。这个简单的学生管理系统允许您查看学生列表、查看学生详情、添加新学生、编辑学生信息以及删除学生记录。
请注意,这只是一个简单的示例,您可以根据自己的需求和复杂性来扩展和完善它。在实际应用中,您还需要添加表单验证、权限控制、样式美化等等。同时,确保在实际项目中使用 Django 提供的内置功能,例如 ListView
和 DetailView
,来