写一个基于django的前后端分离的学生管理系统
时间: 2023-09-02 22:15:38 浏览: 219
这是一个比较大的项目,需要涉及到前后端分离、数据库设计、用户认证、API接口设计等多方面知识。以下是一个简单的步骤:
1. 确定数据库设计,包括学生信息表、用户表等;
2. 搭建Django项目,并安装各种必要的依赖包;
3. 编写用户认证系统,包括注册、登录、注销等功能;
4. 编写API接口,包括获取学生列表、添加学生、修改学生信息、删除学生等;
5. 前端使用Vue或React等框架,调用后端API接口实现前后端分离;
6. 编写前端页面,包括学生列表展示、添加学生表单、修改学生信息表单等。
具体实现细节可以参考下面的代码示例:
1. 数据库设计
在Django项目中,定义学生信息模型和用户模型:
```python
from django.db import models
from django.contrib.auth.models import AbstractUser
class Student(models.Model):
name = models.CharField(max_length=50)
age = models.IntegerField()
gender = models.CharField(max_length=10)
class_number = models.CharField(max_length=50)
class User(AbstractUser):
name = models.CharField(max_length=50)
phone = models.CharField(max_length=11)
```
2. 用户认证系统
在Django项目中,使用Django自带的认证系统实现用户注册、登录、注销等功能:
```python
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, redirect
def register(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
return redirect('login')
else:
form = UserCreationForm()
return render(request, 'register.html', {'form': form})
def user_login(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
return redirect('student_list')
else:
return render(request, 'login.html', {'error': 'Invalid login'})
else:
return render(request, 'login.html')
@login_required
def user_logout(request):
logout(request)
return redirect('login')
```
3. API接口设计
在Django项目中,使用Django REST framework实现API接口:
```python
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated
from .serializers import StudentSerializer
from .models import Student
class StudentViewSet(viewsets.ModelViewSet):
permission_classes = [IsAuthenticated]
queryset = Student.objects.all()
serializer_class = StudentSerializer
```
4. 前端页面
使用Vue框架编写前端页面,调用后端API接口实现前后端分离:
```html
<template>
<div>
<h1>学生列表</h1>
<table>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>班级</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="student in students" :key="student.id">
<td>{{ student.name }}</td>
<td>{{ student.age }}</td>
<td>{{ student.gender }}</td>
<td>{{ student.class_number }}</td>
<td>
<button @click="editStudent(student)">编辑</button>
<button @click="deleteStudent(student)">删除</button>
</td>
</tr>
</tbody>
</table>
<button @click="addStudent">添加学生</button>
</div>
</template>
<script>
import axios from 'axios'
export default {
name: 'StudentList',
data() {
return {
students: []
}
},
mounted() {
this.getStudents()
},
methods: {
getStudents() {
axios.get('/api/student/')
.then(response => this.students = response.data)
},
addStudent() {
this.$router.push('/add-student')
},
editStudent(student) {
this.$router.push(`/edit-student/${student.id}`)
},
deleteStudent(student) {
axios.delete(`/api/student/${student.id}/`)
.then(() => this.getStudents())
}
}
}
</script>
```
以上仅是一个简单的示例,实际项目中需要考虑更多的细节。
阅读全文