crwed 6 months ago
parent 22b3305fa0
commit f417ebcb4a

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

@ -0,0 +1,6 @@
from django.apps import AppConfig
class App01Config(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'app01'

@ -0,0 +1,64 @@
# Generated by Django 3.2.13 on 2022-06-13 12:20
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='AdminInfo',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('username', models.CharField(max_length=32, verbose_name='用户名')),
('password', models.CharField(max_length=64, verbose_name='密码')),
],
),
migrations.CreateModel(
name='CourseInfo',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('course_name', models.CharField(max_length=32, verbose_name='课程名称')),
('course_brief', models.CharField(default='', max_length=128, verbose_name='课程介绍')),
('class_hour', models.SmallIntegerField(verbose_name='课时')),
('points', models.SmallIntegerField(verbose_name='学分')),
],
),
migrations.CreateModel(
name='Department',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('department_name', models.CharField(max_length=32, verbose_name='学院名')),
],
),
migrations.CreateModel(
name='Select',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('course', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app01.courseinfo', verbose_name='课程')),
],
),
migrations.CreateModel(
name='StuInfo',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('username', models.CharField(max_length=32, unique=True, verbose_name='用户名')),
('password', models.CharField(max_length=64, verbose_name='密码')),
('name', models.CharField(max_length=32, verbose_name='姓名')),
('gender', models.SmallIntegerField(choices=[(1, ''), (2, '')], verbose_name='性别')),
('course', models.ManyToManyField(through='app01.Select', to='app01.CourseInfo')),
('department', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app01.department', verbose_name='学院')),
],
),
migrations.AddField(
model_name='select',
name='student',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app01.stuinfo', verbose_name='学生'),
),
]

@ -0,0 +1,39 @@
from django.db import models
# Create your models here.
class StuInfo(models.Model):
username = models.CharField(verbose_name='用户名', max_length=32, unique=True)
password = models.CharField(verbose_name='密码', max_length=64)
name = models.CharField(verbose_name='姓名', max_length=32)
gender_choices = (
(1, ''),
(2, ''),
)
gender = models.SmallIntegerField(verbose_name='性别', choices=gender_choices)
department = models.ForeignKey(verbose_name='学院', to='Department', to_field='id', on_delete=models.CASCADE)
course = models.ManyToManyField(to='CourseInfo', through='Select')
class Department(models.Model):
department_name = models.CharField(verbose_name='学院名', max_length=32)
def __str__(self):
return self.department_name
class CourseInfo(models.Model):
course_name = models.CharField(verbose_name='课程名称', max_length=32)
course_brief = models.CharField(verbose_name='课程介绍', max_length=128, default='')
class_hour = models.SmallIntegerField(verbose_name='课时')
points = models.SmallIntegerField(verbose_name='学分')
class AdminInfo(models.Model):
username = models.CharField(verbose_name='用户名', max_length=32)
password = models.CharField(verbose_name='密码', max_length=64)
class Select(models.Model):
student = models.ForeignKey(StuInfo, verbose_name='学生', on_delete=models.CASCADE)
course = models.ForeignKey(CourseInfo, verbose_name='课程', on_delete=models.CASCADE)

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

After

Width:  |  Height:  |  Size: 434 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -0,0 +1,38 @@
{% load static %}
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title> - 信息管理系统 - </title>
<!-- 最新 Bootstrap 核心 CSS 文件 --><link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
<!-- 可选的Bootstrap主题文件一般不用引入 --><link rel="stylesheet" href="{% static 'css/bootstrap-theme.min.css' %}">
<link href="{% static 'css/font-awesome.min.css' %}" rel="stylesheet">
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 --><script src="{% static 'js/jquery.min.js' %}"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 --><script src="{% static 'js/bootstrap.min.js' %}"></script>
</head>
<body>
<div class="container">
<div class="row">
<nav class="navbar navbar-default" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<a class="navbar-brand" href="#">信息管理系统</a>
</div>
{% block nav_content %}
{% endblock %}
</nav>
</div>
{% block main_content %}
{% endblock %}
<div class="row">
<div class="well text-center">
&copy; <a href="#"></a>
</div>
</div>
</div>
</body>
</html>

@ -0,0 +1,24 @@
{% extends 'base.html' %}
{% block nav_content %}
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav" id="navbar-1">
<li id="1"><a href="/manage/homepage">首页</a></li>
<li id="2"><a href="/manage/stu/list/" class="href">学生管理</a></li>
<li id="3"><a href="/manage/course/list/" class="href">课程管理</a></li>
<li id="4"><a href="/manage/select/list/" class="href">选课管理</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">管理员<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="/">退出</a></li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
{% endblock %}
{% block main_content %}
{% endblock %}

@ -0,0 +1,27 @@
{% extends 'base_admin.html' %}
{% block main_content %}
<div class="row">
<div class="panel panel-default">
<div class="panel-heading">
<h3>新增课程信息</h3>
</div>
<div class="panel-body" style="overflow-x: scroll; width:100%;">
<form action="#" method="post" novalidate>
{% csrf_token %}
{% for field in form %} <!--field代表每个字段的input框-->
<div class="form-group">
<label class='control-label' for="{{ field.id_for_label }}">{{ field.label }}</label>
<div>
{{ field }}<span style="color: red">{{ field.errors.0 }}</span>
</div>
</div>
{% endfor %}
<div class="form-group">
<input class="btn btn-primary" type="submit" value="添加">
<input type="reset" class="btn btn-default" value="重置">
</div>
</form>
</div>
</div>
</div>
{% endblock %}

@ -0,0 +1,27 @@
{% extends 'base_admin.html' %}
{% block main_content %}
<div class="row">
<div class="panel panel-default">
<div class="panel-heading">
<h3>修改课程信息</h3>
</div>
<div class="panel-body" style="overflow-x: scroll; width:100%;">
<form action="#" method="post">
{% csrf_token %}
{% for field in form %} <!--field代表每个字段的input框-->
<div class="form-group">
<label class='control-label' for="{{ field.id_for_label }}">{{ field.label }}</label>
<div>
{{ field }}<span class="errors">{{ field.errors.0 }}</span>
</div>
</div>
{% endfor %}
<div class="form-group">
<input class="btn btn-primary" type="submit" value="修改">
<input type="reset" class="btn btn-default" value="重置">
</div>
</form>
</div>
</div>
</div>
{% endblock %}

@ -0,0 +1,68 @@
{% extends 'base_admin.html' %}
{% block main_content %}
<div class="row">
<div class="panel panel-default">
<div class="panel-heading">
<form action="#" class="form-inline">
<input type="text" class="form-control" name="key" id="key" placeholder="按姓名查找">
<input type="submit" class="btn btn-default" value="搜索">
</form>
</div>
<div class="panel-body" style="overflow-x: scroll; width:100%;">
<a href="/manage/course/add/" class="btn btn-success" style="margin-bottom: 10px">新增课程</a>
<table class="table table-hover" style="width:150%; height:100px; text-align:left" >
<tr>
<th>#</th>
<th>课程名称</th>
<th>课程简介</th>
<th>课时</th>
<th>学分</th>
<th>操作</th>
</tr>
{% for course in courses %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ course.course_name }}</td>
<td>{{ course.course_brief }}</td>
<td>{{ course.class_hour }}</td>
<td>{{ course.points }}</td>
<td>
<a href="/manage/course/edit/{{ course.id }}/" class="btn btn-default btn-xs" title="编辑"><i class="fa fa-edit"></i>编辑</a>
<a href="/manage/course/del/{{ course.id }}/" class="btn btn-default btn-xs" title="删除"><i class="fa fa-trash"></i>删除</a>
</td>
</tr>
{% endfor %}
</table>
<nav aria-label="Page navigation">
<ul class="pagination">
{% if courses.has_previous %}
<li class="prev-page"><a href="?page={{ courses.previous_page_number }}">上一页</a></li>
{% else %}
<li class="prev-page"></li>
{% endif %}
{% for num in courses.paginator.page_range %}
{% if num %}
{% ifequal num courses.number %}
<li class="active"><span>{{ num }}</span></li>
{% else %}
<li><a href="?page={{ num }}">{{ num }}</a></li>
{% endifequal %}
{% else %}
<li class="disabled"><span>...</span></li>
{% endif %}
{% endfor %}
{% if courses.has_next %}
<li class="next-page"><a href="?page={{ courses.next_page_number }}">下一页</a></li>
{% else %}
<li class="next-page"></li>
{% endif %}
</ul>
</nav>
</div>
</div>
</div>
{% endblock %}

@ -0,0 +1,8 @@
{% extends 'base.html' %}
{% block main_content %}
<div class="jumbotron">
<h1>欢迎使用</h1>
<h1>信息管理系统</h1>
<p align="right"><a class="btn btn-lg btn-success" href="/login/" role="button">登录</a></p>
</div>
{% endblock %}

@ -0,0 +1,13 @@
{% extends 'base_admin.html' %}
{% block main_content %}
<div class="jumbotron">
<h1>信息管理系统</h1>
<h1>管理员页面</h1>
<p align="right">
<a class="btn btn-lg btn-success" href="/manage/stu/list/" role="button">学生管理</a>
<a class="btn btn-lg btn-success" href="/manage/course/list/" role="button">课程管理</a>
<a class="btn btn-lg btn-success" href="#" role="button">选课管理</a>
</p>
</div>
{% endblock %}

@ -0,0 +1,46 @@
{% extends 'base.html' %}
{% block main_content %}
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-default">
<div class="panel-heading">
<h4>人员登录</h4>
</div>
<div class="panel-body">
<form action="#" class="form-horizontal" method="post">
{% csrf_token %}
<div class="col-md-10 col-md-offset-1">
<div class="form-group">
<label for="role" class="col-md-2">&#12288色</label>
<div class="col-md-10">
<select name="role" id="role" class="form-control">
<option value="0">学生</option>
<option value="1">管理员</option>
</select>
</div>
</div>
<div class="form-group">
<label for="username" class="col-md-2">用户名</label>
<div class="col-md-10">
<input type="text" class="form-control" name="username" id="username">
</div>
</div>
<div class="form-group">
<label for="pwd" class="col-md-2">&#12288码</label>
<div class="col-md-10">
<input type="password" class="form-control" name="pwd" id="pwd">
</div>
</div>
<div class="form-group">
<div class="col-md-10 col-md-offset-2">
<input class="btn btn-primary" type="submit" value="登录">
<input type="reset" class="btn btn-default" value="重置">
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
{% endblock %}

@ -0,0 +1,76 @@
{% extends 'base_admin.html' %}
{% block main_content %}
<div class="row">
<div class="panel panel-default">
<div class="panel-heading">
<form action="#" class="form-inline">
<input type="text" class="form-control" name="key" id="key" placeholder="按课程查找">
<input type="submit" class="btn btn-default" value="搜索">
</form>
</div>
<div class="panel-body" style="overflow-x: scroll; width:100%;">
<table class="table table-hover" style="width:150%; height:100px; text-align:left">
<tr>
<th>#</th>
<th>课程</th>
<th>姓名</th>
<th>操作</th>
</tr>
{# {% for k,vs in queryset.items %}#}
{# {% for v in vs %}#}
{# <tr>#}
{# <td>{{ forloop.counter }}</td>#}
{# <td>{{ v.course_name }}</td>#}
{# <td>{{ k }}</td>#}
{##}
{# <td>#}
{# <a href="/manage/select/del/{{ v.id }}/{{ k }}/" class="btn btn-default btn-xs" title="删除"><i class="fa fa-trash"></i>删除</a>#}
{# </td>#}
{# </tr>#}
{# {% endfor %}#}
{# {% endfor %}#}
{% for select in selects %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ select.course.course_name }}</td>
<td>{{ select.student.name }}</td>
<td>
<a href="/manage/select/del/{{ select.id }}/" class="btn btn-default btn-xs" title="删除"><i
class="fa fa-trash"></i>删除</a>
</td>
</tr>
{% endfor %}
</table>
<nav aria-label="Page navigation">
<ul class="pagination">
{% if selects.has_previous %}
<li class="prev-page"><a href="?page={{ selects.previous_page_number }}">上一页</a></li>
{% else %}
<li class="prev-page"></li>
{% endif %}
{% for num in selects.paginator.page_range %}
{% if num %}
{% ifequal num selects.number %}
<li class="active"><span>{{ num }}</span></li>
{% else %}
<li><a href="?page={{ num }}">{{ num }}</a></li>
{% endifequal %}
{% else %}
<li class="disabled"><span>...</span></li>
{% endif %}
{% endfor %}
{% if selects.has_next %}
<li class="next-page"><a href="?page={{ selects.next_page_number }}">下一页</a></li>
{% else %}
<li class="next-page"></li>
{% endif %}
</ul>
</nav>
</div>
</div>
</div>
{% endblock %}

@ -0,0 +1,27 @@
{% extends 'base_admin.html' %}
{% block main_content %}
<div class="row">
<div class="panel panel-default">
<div class="panel-heading">
<h3>新增学生信息</h3>
</div>
<div class="panel-body" style="overflow-x: scroll; width:100%;">
<form action="#" method="post">
{% csrf_token %}
{% for field in form %} <!--field代表每个字段的input框-->
<div class="form-group">
<label class='control-label' for="{{ field.id_for_label }}">{{ field.label }}</label>
<div>
{{ field }}<span class="errors">{{ field.errors.0 }}</span>
</div>
</div>
{% endfor %}
<div class="form-group">
<input class="btn btn-primary" type="submit" value="添加">
<input type="reset" class="btn btn-default" value="重置">
</div>
</form>
</div>
</div>
</div>
{% endblock %}

@ -0,0 +1,27 @@
{% extends 'base_admin.html' %}
{% block main_content %}
<div class="row">
<div class="panel panel-default">
<div class="panel-heading">
<h3>修改学生信息</h3>
</div>
<div class="panel-body" style="overflow-x: scroll; width:100%;">
<form action="#" method="post">
{% csrf_token %}
{% for field in form %} <!--field代表每个字段的input框-->
<div class="form-group">
<label class='control-label' for="{{ field.id_for_label }}">{{ field.label }}</label>
<div>
{{ field }}<span class="errors">{{ field.errors.0 }}</span>
</div>
</div>
{% endfor %}
<div class="form-group">
<input class="btn btn-primary" type="submit" value="修改">
<input type="reset" class="btn btn-default" value="重置">
</div>
</form>
</div>
</div>
</div>
{% endblock %}

@ -0,0 +1,43 @@
{% extends 'base.html' %}
{% block nav_content %}
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav" id="navbar-1">
<li id="0"><a href="/stu/index/{{ sid }}/" class="href">选课页面</a></li>
<li id="1"><a href="/stu/selected/{{ sid }}/" class="href">已选课程</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">学生<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="/">退出</a></li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
{% endblock %}
{% block main_content %}
<div class="row">
{% for course in courses %}
<div class="col-md-4 col-sm-6">
<div class="thumbnail">
<img style="height: 200px; width: 100%; display: block;"
src="/static/img/{{ course.course_name }}.jpg">
<div class="caption">
<h3>{{ course.course_name }}</h3>
<p>{{ course.class_hour }}学时 {{ course.points }}学分</p>
<p>{{ course.course_brief }}</p>
<form action="#" method="post">
{% csrf_token %}
<div class="form-group">
<input name='sid' type="text" value="{{ sid }}" hidden>
<input name="cid" type='text' value="{{ course.id }}" hidden>
<input class="btn btn-primary" type="submit" value="选课">
</div>
</form>
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock %}

@ -0,0 +1,70 @@
{% extends 'base_admin.html' %}
{% block main_content %}
<div class="row">
<div class="panel panel-default">
<div class="panel-heading">
<form action="#" class="form-inline">
<input type="text" class="form-control" name="key" id="key" placeholder="按姓名查找">
<input type="submit" class="btn btn-default" value="搜索">
</form>
</div>
<div class="panel-body" style="overflow-x: scroll; width:100%;">
<a href="/manage/stu/add/" class="btn btn-success" style="margin-bottom: 10px">新增学生</a>
<table class="table table-hover" style="width:150%; height:100px; text-align:left" >
<tr>
<th>#</th>
<th>姓名</th>
<th>性别</th>
<th>学院</th>
<th>用户名</th>
<th>密码</th>
<th>操作</th>
</tr>
{% for student in students %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ student.name }}</td>
<td>{{ student.get_gender_display }}</td>
<td>{{ student.department.department_name }}</td>
<td>{{ student.username }}</td>
<td>{{ student.password }}</td>
<td>
<a href="/manage/stu/edit/{{ student.id }}/" class="btn btn-default btn-xs" title="编辑"><i class="fa fa-edit"></i>编辑</a>
<a href="/manage/stu/del/{{ student.id }}/" class="btn btn-default btn-xs" title="删除"><i class="fa fa-trash"></i>删除</a>
</td>
</tr>
{% endfor %}
</table>
<nav aria-label="Page navigation">
<ul class="pagination">
{% if students.has_previous %}
<li class="prev-page"><a href="?page={{ students.previous_page_number }}">上一页</a></li>
{% else %}
<li class="prev-page"></li>
{% endif %}
{% for num in students.paginator.page_range %}
{% if num %}
{% ifequal num students.number %}
<li class="active"><span>{{ num }}</span></li>
{% else %}
<li><a href="?page={{ num }}">{{ num }}</a></li>
{% endifequal %}
{% else %}
<li class="disabled"><span>...</span></li>
{% endif %}
{% endfor %}
{% if students.has_next %}
<li class="next-page"><a href="?page={{ students.next_page_number }}">下一页</a></li>
{% else %}
<li class="next-page"></li>
{% endif %}
</ul>
</nav>
</div>
</div>
</div>
{% endblock %}

@ -0,0 +1,44 @@
{% extends 'base.html' %}
{% block nav_content %}
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav" id="navbar-1">
<li id="0"><a href="/stu/index/{{ sid }}/" class="href">选课页面</a></li>
<li id="1"><a href="/stu/selected/{{ sid }}/" class="href">已选课程</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">学生<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="/">退出</a></li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
{% endblock %}
{% block main_content %}
<div class="row">
{% for course in courses %}
<div class="col-md-4 col-sm-6">
<div class="thumbnail">
<img style="height: 200px; width: 100%; display: block;"
src="/static/img/{{ course.course_name }}.jpg">
<div class="caption">
<h3>{{ course.course_name }}</h3>
<p>{{ course.class_hour }}学时 {{ course.points }}学分</p>
<p>{{ course.course_brief }}</p>
{# 新增退课#}
<form action="#" method="post">
{% csrf_token %}
<div class="form-group">
<input type="text" name="sid" value="{{ sid }}" hidden>
<input type="text" name="cid" value="{{ course.id }}" hidden>
<input type="submit" class="btn btn-primary" value="退选">
</div>
</form>
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock %}

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

@ -0,0 +1,219 @@
from django.shortcuts import render, HttpResponse, redirect
from django.forms import ModelForm
from django.contrib import messages
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger # 分页组件
from app01 import models
# Create your views here.
def index(request):
return render(request, 'homepage.html')
def login(request):
if request.method == 'GET':
return render(request, 'login.html', )
if request.method == 'POST':
# 获取form表单参数
role = request.POST.get('role')
username = request.POST.get('username')
pwd = request.POST.get('pwd')
# 验证身份,用户名,密码
if role == '0': # 如果身份是学生
# 从学生表中查询是否有匹配结果
result = models.StuInfo.objects.filter(username=username, password=pwd)
# 如果用户存在的话
if result.exists():
# 重定向到学生主页把学生ID sid 传到前台页面
return redirect('student_index', sid=result.first().id)
else:
return HttpResponse('failed')
elif role == '1':
result = models.AdminInfo.objects.filter(username=username, password=pwd)
if result.exists():
return redirect('/manage/homepage/')
else:
return HttpResponse('failed')
def manage_homepage(request):
return render(request, 'homepage_admin.html')
def manage_student(request):
search_key = request.GET.get('key', '')
students = models.StuInfo.objects.filter(name__contains=search_key).all()
# 获取当前页数
page = request.GET.get('page')
paginator = Paginator(students, 10)
try:
students = paginator.page(page)
except PageNotAnInteger:
students = paginator.page(1)
except EmptyPage:
students = paginator.page(paginator.num_pages)
return render(request, 'student_manage.html', locals())
def manage_course(request):
search_key = request.GET.get('key', '')
courses = models.CourseInfo.objects.filter(course_name__contains=search_key).all()
# 获取当前页数
page = request.GET.get('page')
paginator = Paginator(courses, 10)
try:
courses = paginator.page(page)
except PageNotAnInteger:
courses = paginator.page(1)
except EmptyPage:
courses = paginator.page(paginator.num_pages)
return render(request, 'course_manage.html',locals())
class StuForm(ModelForm):
class Meta:
model = models.StuInfo
fields = ['username', 'password', 'name', 'gender', 'department']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for field in self.fields.values():
field.widget.attrs['class'] = 'form-control'
def add_student(request):
if request.method == 'GET':
form = StuForm()
return render(request, 'student_add.html', {'form': form})
if request.method == 'POST':
form = StuForm(data=request.POST)
if form.is_valid():
form.save()
return redirect('/manage/stu/list/')
else:
return render(request, 'student_add.html', {'form': form})
def edit_student(request, sid):
queryset = models.StuInfo.objects.filter(id=sid).first()
if request.method == 'GET':
form = StuForm(instance=queryset)
return render(request, 'student_edit.html', {'form': form})
if request.method == 'POST':
form = StuForm(data=request.POST, instance=queryset)
if form.is_valid():
form.save()
return redirect('/manage/stu/list/')
else:
return render(request, 'student_edit.html', {'form': form})
def delete_student(request, sid):
models.StuInfo.objects.filter(id=sid).first().delete()
return redirect('/manage/stu/list/')
class CourseForm(ModelForm):
class Meta:
model = models.CourseInfo
fields = ['course_name', 'course_brief', 'class_hour', 'points']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for field in self.fields.values():
field.widget.attrs['class'] = 'form-control'
def add_course(request):
if request.method == 'GET':
form = CourseForm()
return render(request, 'course_add.html', {'form': form})
if request.method == 'POST':
form = CourseForm(data=request.POST)
if form.is_valid():
form.save()
return redirect('/manage/course/list/')
else:
return render(request, 'course_add.html', {'form': form})
def edit_course(request, cid):
queryset = models.CourseInfo.objects.filter(id=cid).first()
if request.method == 'GET':
form = CourseForm(instance=queryset)
return render(request, 'course_edit.html', {'form': form})
if request.method == 'POST':
form = CourseForm(data=request.POST, instance=queryset)
if form.is_valid():
form.save()
return redirect('/manage/course/list/')
else:
return render(request, 'course_edit.html', {'form': form})
def delete_course(request, cid):
models.CourseInfo.objects.filter(id=cid).first().delete()
return redirect('/manage/course/list/')
def manage_select(request):
# student_objs = models.StuInfo.objects.all()
# queryset = {}
# for obj in student_objs:
# queryset[obj.name] = obj.course.all()
# 获取当前页数
selects = models.Select.objects.all()
print(selects)
page = request.GET.get('page')
paginator = Paginator(selects, 10)
try:
selects = paginator.page(page)
except PageNotAnInteger:
selects = paginator.page(1)
except EmptyPage:
selects = paginator.page(paginator.num_pages)
return render(request, 'select_manage.html', locals())
def delete_select(request, id):
models.Select.objects.filter(id=id).first().delete()
return redirect('/manage/select/list/')
def student_index(request, sid):
if request.method == 'GET':
queryset = models.CourseInfo.objects.all()
return render(request, 'student_index.html', {'courses': queryset, 'sid': sid})
if request.method == 'POST':
course_id = request.POST.get('cid')
student_id = request.POST.get('sid')
course_obj = models.CourseInfo.objects.get(pk=course_id)
student_obj = models.StuInfo.objects.get(pk=student_id)
# 添加学生选课信息
student_obj.course.add(course_obj)
return HttpResponse('success')
def student_selected(request, sid):
if request.method == 'GET':
student_obj = models.StuInfo.objects.filter(pk=sid).first()
queryset = student_obj.course.all()
for q in queryset:
print(q)
return render(request, 'student_select.html', {'courses': queryset, 'sid': sid})
# 新增退课
if request.method == 'POST':
course_id = request.POST.get('cid')
student_id = request.POST.get('sid')
models.Select.objects.filter(student_id=student_id,course_id=course_id).delete()
messages.success(request,'退选成功')
return redirect('student_selected', sid=sid)

Binary file not shown.

@ -0,0 +1,16 @@
"""
ASGI config for djangoProject project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
"""
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djangoProject.settings')
application = get_asgi_application()

@ -0,0 +1,135 @@
"""
Django settings for djangoProject project.
Generated by 'django-admin startproject' using Django 3.2.13.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-flnkk=dk$pn_fnnc1!jum1#z+gs$9rgxff&bc40cqkagnghnk-'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
# 新增app就要安装应用
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app01.apps.App01Config' # 注册了App01这个应用
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'djangoProject.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'djangoProject.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
# sqlite3数据库连接设置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
'CONN_MAX_AGE': 100,
}
}
# MySQL数据库连接设置
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.mysql',
# 'NAME':'django_prj',
# 'USER': 'root',
# 'PASSWORD': 'root',
# 'HOST': 'localhost',
# 'PORT': '3306',
# }
# }
# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
# LANGUAGE_CODE = 'en-us'
# 设置为汉语
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = '/static/'
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

@ -0,0 +1,62 @@
"""djangoProject URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
# 主页登录两个页面的URL
# 主页
path('', views.index),
# 登录
path('login/', views.login),
# 学生选课页面学生已选页面的URL
# 学生选课页面
path('stu/index/<int:sid>/', views.student_index, name='student_index'),
# 已选页面
path('stu/selected/<int:sid>/', views.student_selected, name='student_selected'),
# 管理员主页,学生信息查增改删,课程信息查增
# 管理员首页
path('manage/homepage/', views.manage_homepage),
# 学生信息列表页面
path('manage/stu/list/', views.manage_student),
# 学生信息添加页面
path('manage/stu/add/', views.add_student),
# 学生信息编辑页面
path('manage/stu/edit/<int:sid>/', views.edit_student),
# 学生信息删除url
path('manage/stu/del/<int:sid>/', views.delete_student),
# 课程列表页面
path('manage/course/list/', views.manage_course),
# 课程添加页面
path('manage/course/add/', views.add_course),
# 课程信息编辑页面
path('manage/course/edit/<int:cid>/', views.edit_course),
# 课程信息删除url
path('manage/course/del/<int:cid>/', views.delete_course),
# 选课列表页面
path('manage/select/list/', views.manage_select),
# 删除选课信息url
path('manage/select/del/<int:id>/', views.delete_select),
]

@ -0,0 +1,16 @@
"""
WSGI config for djangoProject project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djangoProject.settings')
application = get_wsgi_application()

@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djangoProject.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
Loading…
Cancel
Save