1. 程式人生 > >多對多作為外鍵,getset方法實現

多對多作為外鍵,getset方法實現

models ble -s ret lec min ren 沒反應 classes

get_class.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div><a href="/add_classes.html">添加</a></div>
<div> <table border="1">
    <thead>
    <tr>
        <th>ID</th>
        <th>名稱</th>
        <th>任課老師</th>
<th>操作</th> </tr> </thead> <tbody> {% for row in cls_list %} <tr> <td>{{ row.id }}</td> <td>{{ row.title }}</td> <td>{% for item in row.m.all %} <span>{{ item.name }}</span> {
% endfor %}</td>
<td><a href="del_classes.html?nid={{ row.id }}">刪除</a> | <a href="edit_classes.html?nid={{ row.id }}">修改</a> | <a href="set_teacher.html?nid={{ row.id }}">分配老師</a> </td> </tr> {
% endfor %} </tbody> </table></div> </body> </html>

url.py

from django.conf.urls import url
from django.contrib import admin
from app.views import classes
from app.views import student
urlpatterns = [
    url(r^admin/, admin.site.urls),
    url(r^classes.html$,classes.get_classes),#不加^訪問點擊頁面沒反應
    url(r^add_classes.html$,classes.add_classes),
    url(r^del_classes.html$, classes.del_classes),
    url(r^edit_classes.html$,classes.edit_classes),
    url(r^set_teacher.html$, classes.set_teacher),

    url(r^student$, student.get_student),
    url(r^add_student$, student.add_student),
    url(r^del_student$, student.del_student),
    url(r^edit_student$,student.edit_student),

]

class.py

from django.shortcuts import render
from django.shortcuts import redirect
from app import models
def get_classes(request):

    cls_list=models.Classes.objects.all()

    return render(request,get_classes.html,{cls_list:cls_list})

def add_classes(request):
    if request.method == GET:
        return render(request, add_classes.html)
    elif request.method == POST:
        title1=request.POST.get("title")
        models.Classes.objects.create(title=title1)
        return redirect(/classes.html)
         #url路徑不是html文件

def del_classes(request):
    nid=request.GET.get("nid")
    models.Classes.objects.filter(id=nid).delete()
    return redirect("/classes.html")

def edit_classes(request):
    if request.method=="GET":
        nid=request.GET.get(nid)
        obj=models.Classes.objects.filter(id=nid).first()
        return render(request,edit_classes.html,{obj:obj})
    elif request.method=="POST":
        nid = request.GET.get(nid)
        title=request.POST.get(abc)
        models.Classes.objects.filter(id=nid).update(title=title)
        return redirect("/classes.html")
def set_teacher(request):
    if request.method==GET:
        nid=request.GET.get(nid)
        cls_obj=models.Classes.objects.filter(id=nid).first()
        cls_teacher_list=cls_obj.m.all().values_list(id,name)
        id_list=list(zip(*cls_teacher_list))[0] if list(zip(*cls_teacher_list)) else []
        all_teacher_list=models.Teacheres.objects.all()
        return render(request,set_teacher.html,
                      {id_list:id_list,
                       all_teacher_list:all_teacher_list,
                       nid:nid
                                                  })
    elif request.method==POST:
        nid=request.GET.get(nid)
        ids=request.POST.getlist(teacher_ids)
        obj=models.Classes.objects.filter(id=nid).first()
        obj.m.set(ids)
        return redirect("/classes.html")

set_teacher.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{% csrf_token %}
<form action="set_teacher.html?nid={{ nid }}" method="POST">
<select name="teacher_ids" id="" multiple size="10">
{#<select name="" id="">#}
    {% for item in all_teacher_list %}
        {% if item.id in id_list %}
            <option value="{{ item.id }}"selected="selected">{{ item.name }}</option>
        {% else %}
        <option value="{{ item.id }}">{{ item.name }}</option>
{% endif %}
{% endfor %}

</select>
    <input type="submit"value="提交">
</form>
</body>
</html>

多對多作為外鍵,get\set方法實現