• 【笔记】Django的ORM之删和改


    【笔记】Django的ORM之删和改

    一 删除操作

    1.视图层

    <table border="1">
        <thead>
        <tr>
            <th>序号</th>
            <th>ID</th>
            <th>出版社名称</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        {% for publisher in publisher_list %}
            <tr>
            <td>{{ forloop.counter }}</td>
            <td>{{ publisher.id }}</td>
            <td>{{ publisher.name }}</td>
           <td><ahref="/delete_publisher/?id={{ publisher.id }}">删除</a></td>
            <td><ahref="/edit_publisher/?id={{ publisher.id }}">编辑</a></td>
            </tr>
        {% endfor %}
    
        </tbody>
    </table>
    

    2. 逻辑层

    通过GET方法向服务端传送要删除数据的id;

    def delete_publisher(request):
        # 通过GET方法获取id
        del_id = request.GET.get('id', None)
        # 如果取到id,删除该数据并跳转到列表页,否则提示失败
        if del_id:
            # 通过id拿到药删除的数据对象
            del_obj = models.Publisher.objects.get(id=del_id)
            del_obj.delete()
            return redirect('/publisher_list/')
        else:
            return HttpResponse('删除失败,该出版社不存在')
    

    二 修改操作

    1. 视图层

    与上面一样

    2. 逻辑层

    def edit_publisher(request):
        # 如果是POST请求,说明是要更新出版社名称
        if request.method == 'POST':
            # 获取要更新的id
            edit_id = request.POST.get("id", None)
            # 获取新的名字
            new_name = request.POST.get("publisher_name")
            # 通过id获得出版社对象
            publisher_obj = models.Publisher.objects.get(id=edit_id)
            # 赋值新名字
            publisher_obj.name = new_name
            # 提交到数据库中执行
            publisher_obj.save()
    
            return redirect('/publisher_list/')
    
        edit_id = request.GET.get("id", None)
        if edit_id:
            publisher_obj = models.Publisher.objects.get(id=edit_id)
            return render(request, 'edit_publisher.html', {"publisher":publisher_obj})
        else:
            return HttpResponse('编辑的出版社不存在!')
    

    三 其他

    1. 下拉框

    <form action="/edit_book/" method="post">
        <input type="text" style="display: none" name="id" value="{{ edit_obj.id }}">
        <p>书名:
            <input type="text" name="book_title" value="{{ edit_obj.title }}">
        </p>
        <p>出版社名称:
            <select name="publisher">
                {% for publisher in all_publisher %}
                    {% if edit_obj.publisher.id == publisher.id %}
                       <optionselectedvalue="{{ publisher.id }}">{{ publisher.name }}</option>
                    {% else %}
                       <optionvalue="{{ publisher.id }}">{{ publisher.name }}</option>
                    {% endif %}
                {% endfor %}
            </select>
        </p>
        <p>
            <input type="submit" value="提交">
        </p>
    </form>
    

    2. 对已有数据的数据表添加字段

     

    两种更新字段的方法

    添加字段

    执行python3 manage.py makemigrationspython3 manage.py migrate时,可以通过以下方式找到一个执行窗口,更方便的执行。

     

     

    manage.py执行窗口

    manage.py执行窗口

     

  • 相关阅读:
    连接MySQL错误:Can't connect to MySQL server (10060)
    PHP性状的使用
    PHP interface(接口)的示例代码
    jquery 设置页面元素不可点击、不可编辑、只读(备忘)
    ace_admin_1.3.1 wysiwyg 工具条下拉出不来
    类函数和对象函数 PHP
    PHP 回调、匿名函数和闭包
    simplexml_load_file 抑制警告的直接输出
    jQuery判断当前元素是第几个元素
    hihocoder #1445 : 后缀自动机二·重复旋律5
  • 原文地址:https://www.cnblogs.com/banshaohuan/p/9260671.html
Copyright © 2020-2023  润新知