十一、Django之数据库数据+部门添加/删除/编辑员工

数据库数据

depart_list.html:

{% for obj in queryset %}
   <tr>
      <th>{{ obj.id }}</th>
      <td>{{ obj.title }}</td>
      <td>
          <a class="btn btn-primary btn-xs" href="/depart/{{ obj.id }}/edit/">编辑</a>
          <a class="btn btn-danger btn-xs" href="/depart/delete/?nid={{ obj.id }}">删除</a>
      </td>
   </tr>
{% endfor %}

view.py:

from django.shortcuts import render, redirect
from app01 import models


# Create your views here.

def depart_list(request):
    # 部門列表
    queryset = models.Department.objects.all()

    return render(request, 'depart_list.html', {'queryset': queryset})

部门添加员工

depart_list.html:

<a class="btn btn-success" href="/depart/add/">
     <span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span>
                    新建部门
</a>

urls.py:

添加path("depart/add/", views.depart_add),

views.py:

def depart_add(request):
    # 添加部門
    if request.method == "GET":
        return render(request, 'depart_add.html')

    title = request.POST.get("title")
    models.Department.objects.create(title=title)
    return redirect("/depart/list/")

添加depart_add.html:

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="{% static 'plugins/bootstrap-3.4.1/css/bootstrap.min.css' %}">
    <style>
        .navbar {
            border-radius: 0;
        }
    </style>
</head>
<body>
<nav class="navbar navbar-default">
    <div class="container">
        <div class="nav-header">
            <button type="button" class="navbar-toggle collapse" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar">Toggle navigation</span>
                <span class="icon-bar">Toggle navigation</span>
                <span class="icon-bar">Toggle navigation</span>
            </button>
            <a class="navbar-brand" href="#">联通用户管理系统</a>
        </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li><a href="/list">部门管理</a></li>
                <li><a href="#">Link</a></li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">登录</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                       aria-expanded="false">shanshan<span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">个人资料</a></li>
                        <li><a href="#">我的信息</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">注销</a></li>
                    </ul>
                </li>

            </ul>
        </div>
    </div>
</nav>
<div>
    <div class="container">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">新建部门</h3>
            </div>
            <div class="panel-body">
                <form method="post">
                    {% csrf_token %}
                    <div class="form-group">
                        <label>标题</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" placeholder="标题" name="title">
                        </div>
                    </div>

                         <button type="submit" class="btn btn-primary">提 交</button>
                </form>
            </div>
        </div>
    </div>
</div>
<srcipt src="{% static 'js/jquery-3.6.0.min.js' %}"></srcipt>
<script src="{% static 'plugins/bootstrap-3.4.1/js/bootstrap.min.js' %}"></script>
</body>
</html>

部门删除员工

urls.py:

添加path("depart/delete/", views.depart_delete),

views.py:

def depart_delete(request):
    # 删除部门
    nid=request.GET.get('nid')
    models.Department.objects.filter(id=nid).delete()
    return redirect("depart/list/")

部门编辑员工

urls.py:

# 相当于正则表达式,约束了depart/和edit/之间只能有数字。http://127.0.0.1:8000/depart/2/edit/
    path("depart/<int:nid>/edit/", views.depart_edit)

views.py:

def depart_edit(request, nid):
    # 修改部门
    if request.method == "GET":
        row_object = models.Department.objects.filter(id=nid).first()
        print(row_object.id, row_object.title)
        return render(request, 'depart_edit.html', {"row_object": row_object})
    # 获取用户提交的标题
    title = request.POST.get("title")
    # 根据id找到数据库数据并更新
    models.Department.objects.filter(id=nid).update(title="")
    # 重定向回部门列表
    return redirect("depart/list/")

添加depart_edit.html:

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="{% static 'plugins/bootstrap-3.4.1/css/bootstrap.min.css' %}">
    <style>
        .navbar {
            border-radius: 0;
        }
    </style>
</head>
<body>
<nav class="navbar navbar-default">
    <div class="container">
        <div class="nav-header">
            <button type="button" class="navbar-toggle collapse" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar">Toggle navigation</span>
                <span class="icon-bar">Toggle navigation</span>
                <span class="icon-bar">Toggle navigation</span>
            </button>
            <a class="navbar-brand" href="#">联通用户管理系统</a>
        </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li><a href="/list">部门管理</a></li>
                <li><a href="#">Link</a></li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">登录</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                       aria-expanded="false">shanshan<span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">个人资料</a></li>
                        <li><a href="#">我的信息</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">注销</a></li>
                    </ul>
                </li>

            </ul>
        </div>
    </div>
</nav>
<div>
    <div class="container">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">修改部门</h3>
            </div>
            <div class="panel-body">
                <form method="post">
                    {% csrf_token %}
                    <div class="form-group">
                        <label>标题</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" placeholder="标题" name="title" value="{{ row_object.title }}">
                        </div>
                    </div>

                         <button type="submit" class="btn btn-primary">提 交</button>
                </form>
            </div>
        </div>
    </div>
</div>
<srcipt src="{% static 'js/jquery-3.6.0.min.js' %}"></srcipt>
<script src="{% static 'plugins/bootstrap-3.4.1/js/bootstrap.min.js' %}"></script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shanshandeisu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值