给一个系统,基于Django的学员健康档案管理系统
时间: 2023-12-07 13:05:02 浏览: 158
好的,以下是一个简单的基于Django的学员健康档案管理系统的设计:
1. 数据库模型设计:
```python
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=50)
age = models.IntegerField()
gender = models.CharField(max_length=10)
phone = models.CharField(max_length=20)
email = models.EmailField()
class HealthRecord(models.Model):
student = models.ForeignKey(Student, on_delete=models.CASCADE)
date = models.DateField()
height = models.FloatField()
weight = models.FloatField()
temperature = models.FloatField()
blood_pressure = models.CharField(max_length=20)
```
2. 视图函数和模板设计:
```python
from django.shortcuts import render, redirect
from .models import Student, HealthRecord
from datetime import datetime
def add_student(request):
if request.method == 'POST':
name = request.POST['name']
age = request.POST['age']
gender = request.POST['gender']
phone = request.POST['phone']
email = request.POST['email']
student = Student.objects.create(name=name, age=age, gender=gender, phone=phone, email=email)
return redirect('add_health_record', student_id=student.id)
return render(request, 'add_student.html')
def add_health_record(request, student_id):
student = Student.objects.get(id=student_id)
if request.method == 'POST':
date = datetime.strptime(request.POST['date'], '%Y-%m-%d').date()
height = request.POST['height']
weight = request.POST['weight']
temperature = request.POST['temperature']
blood_pressure = request.POST['blood_pressure']
HealthRecord.objects.create(student=student, date=date, height=height, weight=weight, temperature=temperature, blood_pressure=blood_pressure)
return redirect('student_detail', student_id=student.id)
return render(request, 'add_health_record.html', {'student': student})
def student_detail(request, student_id):
student = Student.objects.get(id=student_id)
health_records = HealthRecord.objects.filter(student=student).order_by('-date')
return render(request, 'student_detail.html', {'student': student, 'health_records': health_records})
```
add_student.html 模板:
```html
<form method="post">
{% csrf_token %}
<label for="name">姓名</label>
<input type="text" id="name" name="name"><br>
<label for="age">年龄</label>
<input type="text" id="age" name="age"><br>
<label for="gender">性别</label>
<input type="text" id="gender" name="gender"><br>
<label for="phone">电话</label>
<input type="text" id="phone" name="phone"><br>
<label for="email">邮箱</label>
<input type="email" id="email" name="email"><br>
<button type="submit">提交</button>
</form>
```
add_health_record.html 模板:
```html
<form method="post">
{% csrf_token %}
<label for="date">日期</label>
<input type="date" id="date" name="date"><br>
<label for="height">身高</label>
<input type="text" id="height" name="height"><br>
<label for="weight">体重</label>
<input type="text" id="weight" name="weight"><br>
<label for="temperature">体温</label>
<input type="text" id="temperature" name="temperature"><br>
<label for="blood_pressure">血压</label>
<input type="text" id="blood_pressure" name="blood_pressure"><br>
<button type="submit">提交</button>
</form>
```
student_detail.html 模板:
```html
<h1>{{ student.name }}</h1>
<p>年龄:{{ student.age }}</p>
<p>性别:{{ student.gender }}</p>
<p>电话:{{ student.phone }}</p>
<p>邮箱:{{ student.email }}</p>
<h2>健康档案</h2>
<ul>
{% for health_record in health_records %}
<li>{{ health_record.date }}:身高{{ health_record.height }}cm,体重{{ health_record.weight }}kg,体温{{ health_record.temperature }}℃,血压{{ health_record.blood_pressure }}</li>
{% endfor %}
</ul>
```
3. URL 配置:
```python
from django.urls import path
from . import views
urlpatterns = [
path('add_student/', views.add_student, name='add_student'),
path('add_health_record/<int:student_id>/', views.add_health_record, name='add_health_record'),
path('student_detail/<int:student_id>/', views.student_detail, name='student_detail'),
]
```
这个系统包含两个主要的模型:学员(Student)和健康档案(HealthRecord),学员可以有多条健康档案记录。视图函数包含添加学员、添加健康档案和查看学员详情三个功能。模板文件负责渲染页面。URL 配置将 URL 映射到对应的视图函数上。
阅读全文