• django单表操作,增、删、改、查


    一、实现:增、删、改、查

    1、获取所有数据显示在页面上

    model.Classes.object.all(),拿到数据后,渲染给前端;前端通过for循环的方式,取出数据。

    目的:通过classes(班级表数据库)里面的字段拿到对应的数据。

    2、添加功能

    配置url分发路由增加一个add_classes.html页面

    写一个def add_classess函数

    在前端写一个a标签,前端页面就可以看到一个添加链接,通过点这个a标签的链接跳转到一个新的add_classess页面

    add_classess.html 页面中实现两个功能:

    form表单 :返回给add_classess.html页面

    input  输入框

    input  提交按钮

    接下来就要接收前端输入的数据:

    if  request.mothod='GET'

    elif

     request.mothod='POST'

    request.POST.get('title')  拿到传过来的班级数据

    然后通过创建的方式,写入对应的title字段数据库中

    方法:models.Classes.objects.create(titile=title)

    再返回给return redirect('/get_classes.html')

    3、删除功能

    配置url路由分发

    加一个操作:

    <th>操作</th>

    一个a标签:

    <a href="/del_classes.html?nid={{ row.id }}">删除</a>

    实现删除操作,就是找到数据库中,对应的id字段(赋值给nid=id),删除掉这个ID字段这行数据,就实现了删除功能。

    4、实现编辑功能

    在get_classes.html里添加一个a标签

    配置路由分发

    写def edit_classes函数

    班级这个输入框前面id不显示,因为id不能被用户修改,所以要隐藏。

    根据id拿到这个对象(id 走get方法),id存放在请求头中发送过去的。

    obj对象里面包含id 和 title ,走post方法,title是放在请求体中发送过去的

    第一次:get拿到id

        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})

    第二次:post拿到id和title

        elif request.method == 'POST':
            nid = request.GET.get('nid')
            title = request.POST.get('title')
            models.Classes.objects.filter(id=nid).update(titile=title)
            return redirect('/get_classes.html')

    综合应用示例:

    models.py

    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
       2
       1
     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)

    urls.py

    """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
    
    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),
    
    ]

    classes.py

    from django.shortcuts import render
    from django.shortcuts import redirect
    from app01 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':
            title = request.POST.get('titile')
            models.Classes.objects.create(titile=title)
            return redirect('/get_classes.html')
    
    
    def del_classes(request):
        nid = request.GET.get('nid')
        models.Classes.objects.filter(id=nid).delete()
        return redirect('/get_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('title')
            models.Classes.objects.filter(id=nid).update(titile=title)
            return redirect('/get_classes.html')

    get_classes.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>
            </tr>
            </thead>
            <tbody>
            {% for row in cls_list %}
                <tr>
                    <td>{{ row.id }}</td>
                    <td>{{ row.titile }}</td>
                    <td>
                        <a href="/del_classes.html?nid={{ row.id }}">删除</a>
                        |
                        <a href="/edit_classes.html?nid={{ row.id }}">编辑</a>
                    </td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>
    </body>
    </html>

    add_classes.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <form action="add_classes.html" method="POST">
        {% csrf_token %}
        <input type="text" name="titile" />
        <input type="submit" value="提交" />
    </form>
    </body>
    </html>

    edit_classes.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
        <form action="/edit_classes.html?nid={{ obj.id }}" method="POST">
            {% csrf_token %}
        <input type="text" name="title" value="{{ obj.titile }}" />
        <input type="submit" value="提交"/>
    </form>
    </body>
    </html>
  • 相关阅读:
    火狐下button标签子元素无法点击
    js里面的this指向
    (转载)http协议的Request Payload 和 Form Data 的区别
    (转载)http压缩 Content-Encoding: gzip
    函数的length属性
    Expires
    Etag 和 If-None-Match
    Mac下升级node到最新版本
    高级函数之函数绑定
    Java数据结构和算法day01 稀疏数组与队列
  • 原文地址:https://www.cnblogs.com/eddycomeon/p/8987625.html
Copyright © 2020-2023  润新知