模型定义
from django.db import models
class Grades(models.Model):
g_name = models.CharField(max_length=20)
create_date = models.DateTimeField()
girl_num = models.IntegerField()
boy_num = models.IntegerField()
isDelete = models.BooleanField(default=False)
def __str__(self):
return self.g_name
class Meta:
db_table = 'grades'
class Students(models.Model):
s_name = models.CharField(max_length=20)
s_gender = models.BooleanField(default=True)
s_age = models.IntegerField()
conetend = models.CharField(max_length=20)
isDlete = models.BooleanField(default=False)
# 关联外键
s_grade = models.ForeignKey("Grades")
def __str__(self):
return self.s_name
class Meta:
db_table = 'students'
url
from django.conf.urls import url
from user import views
urlpatterns = [
url(r'^show_info/',views.show_info,name='show_info'),
url(r'^add_stu/',views.add_stu,name='add_stu'),
url(r'^del_stu/(\d+)/',views.del_stu,name='del_stu'),
url(r'^mod_stu/(\d+)/',views.mod_stu,name='mod_stu'),
url(r'^sel_stu/',views.sel_stu,name='sel_stu'),
]
视图函数定义
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.urls import reverse
from user.models import Students,Grades
def show_info(request):
if request.method == 'GET':
stus = Students.objects.all()
return render(request,'info.html',{'stus':stus})
def add_stu(request):
if request.method == 'GET':
grades = Grades.objects.all()
return render(request,'add.html',{'grades':grades})
if request.method == 'POST':
name = request.POST.get('name')
age = request.POST.get('age')
gender = request.POST.get('sex')
content = request.POST.get('msg')
grade = request.POST.getlist('check')[0]
sgrade = Grades.objects.get(g_name=grade)
Students.objects.create(s_name=name,s_age=age,s_gender=gender,contend=content,s_grade=sgrade)
return HttpResponseRedirect(reverse('user:show_info'))
def del_stu(request,id):
if request.method == 'GET':
Students.objects.get(pk=id).delete()
return HttpResponseRedirect(reverse('user:show_info'))
def mod_stu(request,id):
if request.method == 'GET':
stu = Students.objects.get(pk=id)
grades = Grades.objects.all()
return render(request,'mod.html',{'stu':stu,'grades':grades})
if request.method == 'POST':
name = request.POST.get('name')
age = request.POST.get('age')
gender = request.POST.get('gender')
grade = request.POST.getlist('check')[0]
u_grade = Grades.objects.get(g_name=grade)
Students.objects.filter(pk=id).update(s_name=name,s_age=age,s_gender=gender,s_grade=u_grade)
return HttpResponseRedirect(reverse('user:show_info'))
def sel_stu(request):
if request.method == 'GET':
return render(request,'sel.html')
if request.method == 'POST':
name = request.POST.get('name')
try:
stu = Students.objects.get(s_name=name)
return render(request, 'sel.html', {'stu': stu})
except:
return render(request,'sel.html',{'error':'该学生不存在'})
页面html定义
添加页
{% extends 'base.html' %}
{% block content %}
{% csrf_token %}
姓名:
年龄:
性别:
描述:
{% for grade in grades %}
{{grade.g_name}}
{% endfor %}
{% endblock %}
修改页
{% extends 'base.html' %}
{% block content %}
{% csrf_token %}
姓名:
年龄:
性别:
{% for grade in grades %}
{{grade.g_name}}
{% endfor %}
{% endblock %}
查询页
{% extends 'base.html' %}
{% block content %}
{% csrf_token %}
请输入学生姓名:
{% if stu %}
序号姓名年龄性别简介班级
{{stu.id}}{{stu.s_name}}{{stu.s_age}}{{stu.s_gender}}{{stu.contend}}{{stu.s_grade.g_name}}{% endif %}
{{error}}
{% endblock %}
信息页
{% extends 'base.html' %}
{% block content %}
序号姓名年龄性别班级班级成立时间删除学生修改学生
{% for stu in stus %}
{{forloop.counter}}{{stu.s_name}}{{stu.s_age}}{{stu.s_gender}}{{stu.s_grade.g_name}}{{stu.s_grade.create_date}}删除修改{% endfor %}
增加学生
查询学生
{% endblock %}