一、通过url方式实现多对多的:增加,删除,编辑

代码目录:

urls.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""django_one URL Configuration
 
The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.10/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from app01.views import classes
from app01.views import students
from app01.views import teachers
 
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^get_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'^get_students.html$', students.get_students),
    url(r'^add_students.html$', students.add_students),
    url(r'^del_students.html$', students.del_students),
    url(r'^edit_students.html$', students.edit_students),
 
    url(r'^get_teachers.html$', teachers.get_teachers),
    url(r'^add_teachers.html$', teachers.add_teachers),
    url(r'^del_teachers.html$', teachers.del_teachers),
    url(r'^edit_teachers.html$', teachers.edit_teachers),
 
]

models.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from django.db import models
 
# Create your models here.
 
class Classes(models.Model):
    """
    班级表,男
    """
    titile = models.CharField(max_length=32)
    m = models.ManyToManyField("Teachers")
 
class Teachers(models.Model):
    """
    老师表,女
    """
    name = models.CharField (max_length=32)
 
"""
cid_id  tid_id
 1    1
 1    2
 6    1
 1000  1000
"""
# class C2T(models.Model):
#     cid = models.ForeignKey(Classes)
#     tid = models.ForeignKey(Teachers)
 
class Student(models.Model):
    username = models.CharField(max_length=32)
    age = models.IntegerField()
    gender = models.BooleanField()
    cs = models.ForeignKey(Classes)

teachers.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from django.shortcuts import render
from django.shortcuts import redirect
from app01 import models
 
 
def get_teachers(request):
    cls_list = models.Classes.objects.all()
    return render(request,'get_teachers.html',{'cls_list':cls_list})
 
def add_teachers(request):
    if request.method == "GET":
        teachers_list = models.Teachers.objects.all()
        return render(request, 'add_teachers.html',{'teachers_list':teachers_list})
    elif request.method == "POST":
        nid = request.POST.get('titile')
        # [1,2,3,4,]
        teachers_id = request.POST.getlist('teachers_id')
        # 10
        obj = models.Classes.objects.create(titile = nid)
        # 10 1
        # 10 2
        # 10 3
        # 10 4
        obj.m.add(*teachers_id)
        return redirect('get_teachers.html')
 
 
def del_teachers(request):
    nid = request.GET.get('nid')
    models.Classes.objects.filter(id=nid).delete()
    return redirect('get_teachers.html')
 
 
def edit_teachers(request):
    if request.method == "GET":
        nid = request.GET.get('nid')
        obj = models.Classes.objects.filter(id=nid).first()
        teachers_list = models.Teachers.objects.all()
        return render(request,'edit_teachers.html',{'nid':nid,'obj':obj,'teachers_list':teachers_list})
    elif request.method == "POST":
        n = request.POST.get('nid')
        t = request.POST.get('titile')
        teachers_id = request.POST.getlist('teachers_id')
        obj = models.Classes.objects.filter(id=n).update(
            id=n,
            titile=t,
        )
        obj.m.add(*teachers_id)
        return redirect('get_teachers.html')

添加功能

get_teachers.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <a href="/add_teachers.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.titile }}</td>
                <td>
                    {% for cls in row.m.all %}
                    {{ cls.name }}
                    {% endfor %}
                </td>
                <td>
                    <a href="/del_teachers.html?nid={{ row.id }}">删除</a>
                    |
                    <a href="/edit_teachers.html?nid={{ row.id }}">编辑</a>
                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
</div>
</body>
</html>

添加功能

add_teachers.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="add_teachers.html" method="POST">
    {% csrf_token %}
    <p><input type="text" name="titile" placeholder="班级" /></p>
    <p>
    <select name="teachers_id" multiple="multiple" >
          {% for teachers in teachers_list %}
           <option value="{{ teachers.id}}">{{ teachers.name }}</option>
          {% endfor %}
    </select>
    </p>
    <input type="submit" value="提交" />
</form>
</body>
</html>

编辑功能

edit_teachers.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/edit_teachers.html?nid={{ nid }}" method="POST">
    {% csrf_token %}
     <p><input type="text" name="titile" placeholder="班级" value="{{ obj.titile }}"/></p>
    <p>
    <select name="teachers_id" multiple="multiple" >
          {% for teachers in teachers_list %}
           <option value="{{ teachers.id}}">{{ teachers.name }}</option>
          {% endfor %}
    </select>
    </p>
    <input type="submit" value="提交">
</form>
</body>
</html>

django 多对多 增 删 改 查的更多相关文章

  1. django一对多 增 删 改 查

    实现一对多表查询功能 项目代码: models.py from django.db import models # Create your models here. class Classes(mod ...

  2. django ajax增 删 改 查

    具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...

  3. 好用的SQL TVP~~独家赠送[增-删-改-查]的例子

    以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化.  本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...

  4. iOS FMDB的使用(增,删,改,查,sqlite存取图片)

    iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...

  5. iOS sqlite3 的基本使用(增 删 改 查)

    iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...

  6. ADO.NET 增 删 改 查

    ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...

  7. MVC EF 增 删 改 查

    using System;using System.Collections.Generic;using System.Linq;using System.Web;//using System.Data ...

  8. python基础中的四大天王-增-删-改-查

    列表-list-[] 输入内存储存容器 发生改变通常直接变化,让我们看看下面列子 增---默认在最后添加 #append()--括号中可以是数字,可以是字符串,可以是元祖,可以是集合,可以是字典 #l ...

  9. django单表操作 增 删 改 查

    一.实现:增.删.改.查 1.获取所有数据显示在页面上 model.Classes.object.all(),拿到数据后,渲染给前端;前端通过for循环的方式,取出数据. 目的:通过classes(班 ...

随机推荐

  1. Validation failed for query for method

    问题原因 sql语法,使用@Query("select id, username, usersex, userphone from User where User.usersex = ?1& ...

  2. react中使用antd遇到的问题

    1.less使用报错 less配置修改一般都是1个修改1个增加 test: /\.(css|less)$/, // 修改 // 增加 { loader: require.resolve('less-l ...

  3. 初学者易上手的SSH-struts2 03数据封装

    这一章我们一样来获取数据,看看与上一章有什么不同吧.数据封装也有三种方式.下面我们来一一介绍. 第一种:属性封装. 类就用LoginAction吧.里面有两属性,name,pwd.给这两个属性写上ge ...

  4. 网络3-Jsonp

    解决跨域问题的几种办法 1.Flash (不做讨论) 2.服务器代理中转 3.Jsonp 4.document.domain(针对基础域名相同的情况)bj.58.com document.domain ...

  5. vscode 常用插件

    1. Local History 可以查看本地的修改记录,比较不同 2.TODO插件 可以将有待修改或者完善的地方标记出来,在任务列表中查看, 避免后面忘记, 提高效率. 包含TODO, FIXME. ...

  6. 解决悬浮的<header>、<footer>遮挡内容的处理技巧

    在现在的前端页面中,尤其是移动端,经常会需要将<header>或者是<footer>模块悬浮出来,跟随页面的滑动保持定位在页面的最上方或者是最下方,如下图所示. “回复主题”模 ...

  7. Asp.net core 学习笔记 ( Data protection )

    参考 : http://www.cnblogs.com/xishuai/p/aspnet-5-identity-part-one.html http://cnblogs.com/xishuai/p/a ...

  8. input框输入完回车即可查询事件

    简单有效的方法,随笔记录一下在html设置id <input id="search_sim" type="text" class="form-c ...

  9. 朋友给的IE滚动条

    scrollbar-arrow-color: #f4ae21;  /*图6,三角箭头的颜色*/scrollbar-face-color: #333;  /*图5,立体滚动条的颜色*/scrollbar ...

  10. Scrapy爬取静态页面

    Scrapy爬取静态页面 安装Scrapy框架: Scrapy是python下一个非常有用的一个爬虫框架 Pycharm下: 搜索Scrapy库添加进项目即可 终端下: #python2 sudo p ...