1. 程式人生 > 其它 >Django-學生註冊功能

Django-學生註冊功能

技術標籤:Djangopythondjangopost資料庫

----------------------------------------------------------------------------------

在這裡插入圖片描述

在這裡插入圖片描述

----------------------------------------------------------------------------------

步驟:

http://127.0.0.1:8000/student/
http://127.0.0.1:8000/student/showall/
http://127.0.0.1:8000/student/getstu/?cno=1

----------------------------------------------------------------------------------

models.py

from django.db import models


# Create your models here.
class Clazz(models.Model):
    cno = models.AutoField(primary_key=True)
    cname = models.CharField(max_length=30)


class Course(models.Model):
    course_no = models.AutoField(primary_key=True)
    course_name = models.CharField(
max_length=30) class Student(models.Model): sno = models.AutoField(primary_key=True) sname = models.CharField(max_length=30) cls = models.ForeignKey(Clazz, on_delete=models.CASCADE) cour = models.ManyToManyField(Course) # 根據班級名稱獲取班級物件 def getCls(cname): try: cls = Clazz.
objects.get(cname=cname) except Clazz.DoesNotExist: cls = Clazz.objects.create(cname=cname) return cls # 獲取課程物件列表 def getCourseList(*coursenames): courseList = [] for cn in coursenames: try: c = Course.objects.get(course_name=cn) except Course.DoesNotExist: c = Course.objects.create(course_name=cn) courseList.append(c) return courseList def registerStu(sname, cname, *coursenames): # 1.獲取班級物件 cls = getCls(cname) # 2.獲取課程物件列表 courseList = getCourseList(*coursenames) # 3.插入學生表資料 try: stu = Student.objects.get(sname=sname) except Student.DoesNotExist: stu = Student.objects.create(sname=sname, cls=cls) # 4.插入中間表資料 stu.cour.add(*courseList) return True

----------------------------------------------------------------------------------

views.py

from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
from stu.models import *


def index_view(request):
    if request.method == 'GET':
        return render(request, 'register.html')
    else:
        # 接收請求引數
        sname = request.POST.get('sname', '')
        cname = request.POST.get('clsname', '')
        coursenames = request.POST.getlist('coursename', [])
        # 將資料註冊到資料庫
        flag = registerStu(sname, cname, *coursenames)
        if flag:
            return HttpResponse('註冊成功!')
        return HttpResponse('註冊失敗!')


# 顯示所有班級資訊
def showall_view(request):
    # 查詢班級表中的所有資料
    cls = Clazz.objects.all()
    return render(request, 'showall.html', {'cls': cls})


def getstu_view(request):
    # 獲取班級編號
    con = request.GET.get('cno', '')
    no = int(con)
    # 根據班級編號查詢學生資訊
    stus = Clazz.objects.get(cno=no).student_set.all()
    return render(request, 'stulist.html', {'stus': stus})

----------------------------------------------------------------------------------

register.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>register</title>
</head>
<body>
<form action="/student/" method="post">
    {% csrf_token %}
    <p>
        <label for="sn">姓名:</label> <input name="sname" id="sn"/>
    </p>
    <p>
        <label for="sc">所屬班級::</label>
        <select name="clsname" id="sc">
            <option value="班級1">班級1</option>
            <option value="班級2">班級2</option>
            <option value="班級3">班級3</option>
        </select>
    </p>
    <p>
        <label>選課:</label>
        <input type="checkbox" name="coursename" value="python"/>python &emsp;
        <input type="checkbox" name="coursename" value="c"/>c &emsp;
        <input type="checkbox" name="coursename" value="c++">c++ &emsp;
    </p>
    <p>
        <input type="submit" value="註冊"/>
    </p>
    <p>
        <a href="/student/showall/">showclass</a>
    </p>
</form>
</body>
</html>

----------------------------------------------------------------------------------

showall.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>showall</title>
</head>
<body>
    <table border="1" cellspacing="0" width="500px">
        <tr>
            <th>編號</th>
            <th>班級名稱</th>
            <th>操作</th>
        </tr>
        {% for c in cls %}
            <tr>
                <td>{{ forloop.counter }}</td>
                <td>{{ c.cname }}</td>
                <td>
                    <a href="/student/getstu/?cno={{ c.cno }}">詳情</a>
                </td>
            </tr>
        {% endfor %}




    </table>
</body>
</html>

----------------------------------------------------------------------------------

stulist.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>stulist</title>
</head>
<body>
    <table border="1" cellspacing="0" width="500px">
        <tr>
            <th>編號</th>
            <th>姓名</th>
            <th>班級名稱</th>
            <th>課程名稱</th>
        </tr>
        {% for stu in stus %}
            <tr>
                <td>{{ forloop.counter }}</td>
                <td>{{ stu.sname }}</td>
                <td>{{ stu.cls.cname }}</td>
                <td>
                    {% for cou in stu.cour.all %}
                        {{ cou.course_name }}
                    {% endfor %}

                </td>
            </tr>
        {% endfor %}




    </table>
</body>
</html>