1. 程式人生 > >ORM版,單表查詢

ORM版,單表查詢

req val oct 文件 tle eth set one count

mysql

建學生表及課程表

技術分享圖片

技術分享圖片

添加內容

技術分享圖片

技術分享圖片

view.py

技術分享圖片
from django.shortcuts import render,HttpResponse,redirect
from . import models

# Create your views here.


# 關於課程
# 查所有的課程
def class_list(request):
    class_list  = models.Class.objects.all()
    return render(request,"class_list.html",{"class_list":class_list})

# 刪除操作 def delete_class(request): # 先取到想要刪除的id class_id = request.GET.get(class_id) print(class_id) models.Class.objects.filter(id=class_id).delete() return redirect("/class_list/") # 添加操作 def add_class(request): if request.method == "POST": class_name = request.POST.get("
cname") models.Class.objects.filter().create(cname=class_name) return redirect("/class_list/") return render(request,"add_class.html") # 編輯操作 def edit_class(request): if request.method == "POST": id = request.POST.get("id") new_cname = request.POST.get("cname"
) models.Class.objects.filter(id=id).update(cname = new_cname,) return redirect("/class_list") class_id = request.GET.get("class_id") print(class_id) ret = models.Class.objects.filter(id=class_id) return render(request,"edit_class.html",{"class_info":ret[0]}) # 展示學生信息 def student_list(request): student_list = models.Student.objects.all() return render(request,"student_list.html",{"student_list":student_list}) # 刪除學生信息 def delete_student(request): # 拿到到刪除的ID student_id = request.GET.get(student_id) models.Student.objects.filter(id=student_id).delete() return redirect("/student_list") # 添加學生信息 def add_student(request): if request.method == "POST": student_name = request.POST.get("sname") print(student_name) models.Student.objects.filter().create(sname=student_name) return redirect("/student_list/") return render(request,"add_student.html") # 編輯學生信息 def edit_student(request): if request.method == "POST": student_id = request.POST.get("id") new_sname = request.POST.get("sname") models.Student.objects.filter(id=student_id).update(sname=new_sname) return redirect("/student_list/") # 獲取需要修改的id student_id = request.GET.get("student_id") ret = models.Student.objects.filter(id=student_id) return render(request,"edit_student.html",{"student_info":ret[0]})
邏輯處理

HTML文件

技術分享圖片
---------------------
<!DOCTYPE html>
<html lang="en">
<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>
</head>
<body>
{#{% csrf_token %}  {# 跨站請求偽造保護 #}
<form action="/add_class/" method="post">
    <label for="i1">新課程名稱</label>
    <div>
    <input id="i1" type="text" name="cname">
        <button type="submit">提交</button>
        <button><a href="/class_list/" type="submit">取消</a></button>
    </div>
</form>
</body>
</html>
H添加課程頁面 技術分享圖片
<!DOCTYPE html>
<html lang="en">
<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>
</head>
<body>
    <button type="submit"><a href="/add_class/">添加</a></button>
{#    {% csrf_token %}#}
<form action="class_list.html" METHOD="post">
      {# 跨站請求偽造保護 #}

    <table border="1px">
        <thead>
        <tr>
            <th>#</th>
            <th>班級ID</th>
            <th>班級名稱</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        {% for class in class_list %}
            <tr>
                <td>{{ forloop.counter }}</td>
                <td>{{ class.id }}</td>
                <td>{{ class.cname }}</td>
                <td>
                    <button><a href="/edit_class/?class_id={{ class.id }}">
                        編輯
                    </a></button>
                   <button><a href="/delete_class/?class_id={{ class.id }}">
                        刪除
                    </a></button>
                </td>
            </tr>
        {% endfor %}
        </tbody>

    </table>

</form>
</body>
</html>
課堂列表 技術分享圖片
<!DOCTYPE html>
<html lang="en">
<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>
</head>
<body>
{#{% csrf_token %}#}
<form action="/edit_class/" method="post">
    <h1>編輯課程信息</h1>
    <label for="i1"></label><input id="i1" type="text" name="id" value="{{ class_info.id }}" style="display: none">
    <label for="i1"></label><input id="i1" type="text" name="cname" value="{{ class_info.cname }}">
<div>
    <button type="submit">提交</button>
    <a href="/class_list/"><button type="submit">取消</button></a></div>

</form>
</body>
</html>
編輯課程信息

ORM版,單表查詢