django 静态页面的调用方法 与 部门添加信息 , 部门修改信息 , 部门删除信息 部门列表信息
作者:互联网
1. srtting.py 文件去掉代码
'DIRS': []
2. bootstrap 的引入路径
{% load static %} 绝对路径
#样式路径
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
<script src="{% static 'js/jquery.min.js' %}"></script>
<script src="{% static 'js/bootstrap.min.js' %}"></script>
3. views.py 文件设计代码
from django.shortcuts import render, HttpResponse, redirect from app import models '''部门列表''' def bumen_list(request): # 数据库查询数据 bumen_list = models.BuMen.objects.all() return render(request,'bumen_list.html',{'bumen_list': bumen_list})
'''添加部门''' def bumen_add(request): if request.method == 'GET': return render(request, 'bumen.add.html') # 获取post 传递过来的数据 title = request.POST.get('title') #保存数据 models.BuMen.objects.create(title=title) return redirect('/bumen/list')
'''删除部门 - 不需要html页面''' def bumen_delete(request): # get 方式获取id nid = request.GET.get('nid') # 删除传过来的id models.BuMen.objects.filter(id=nid).delete() return redirect('/bumen/list')
'''修改部门- 另一种传参 ''' def bumen_edit(request, nid): if request.method == 'GET': bumenjieguo = models.BuMen.objects.filter(id=nid).first()
# bumenjieguo返回静态页面 value 值 return render(request, 'bumen_edit.html', {'bumenjieguo':bumenjieguo}) title = request.POST.get('title') models.BuMen.objects.filter(id=nid).update(title=title) return redirect('/bumen/list')
静态页面代码 :
bumen_list.html 部门列表页面
{% load static %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> 部门列表 </title> <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}"> </head> <body> <nav class="navbar navbar-default"> <div class="container-fluid"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">CMS</a> </div> <!-- Collect the nav links, forms, and other content for toggling --> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li class="active"><a href="/bumen/list/">部门 <span class="sr-only">(current)</span></a></li> <li><a href="/userinfo/list/">员工</a></li> </ul> <ul class="nav navbar-nav navbar-right"> <li><a href="#">admin</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">会员管理 <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 class="container"> <div style="margin-bottom: 10px;"> <a href="/bumen/add/" class="btn btn-success"> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 新建部门 </a> </div> <div class="panel panel-default"> <div class="panel-heading"> <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span> 部门列表</div> <table class="table table-bordered"> <thead> <tr> <th>ID</th> <th>部门名称</th> <th>操作</th> </tr> </thead> <tbody> {% for item in bumen_list %} <tr> <th scope="row">{{ item.id }}</th> <td>{{ item.title }}</td> <td> <a href="/bumen/{{ item.id }}/edit" class="btn btn-primary btn-xs">编辑</a> <a href="/bumen/delete/?nid={{ item.id }}" class="btn btn-danger btn-xs">删除</a> </td> </tr> {% endfor %} </tbody> </table> </div> </div> <script src="{% static 'js/jquery.min.js' %}"></script> <script src="{% static 'js/bootstrap.min.js' %}"></script> </body> </html>
部门增加页面 bumen.add.html
{% load static %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> 添加部门 </title> <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}"> </head> <body> <nav class="navbar navbar-default"> <div class="container-fluid"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">CMS</a> </div> <!-- Collect the nav links, forms, and other content for toggling --> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li class="active"><a href="/bumen/list/">部门 <span class="sr-only">(current)</span></a></li> <li><a href="/userinfo/list/">员工</a></li> </ul> <ul class="nav navbar-nav navbar-right"> <li><a href="#">admin</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">会员管理 <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><!-- /.navbar-collapse --> </div><!-- /.container-fluid --> </nav> <div class="container"> <div class="panel panel-default"> <div class="panel-heading"> <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span> 新建部门</div> </div> <form method="post"> {% csrf_token %} <div class="form-group"> <label for="exampleInputEmail1">部门名称</label> <input type="text" class="form-control" placeholder="部门名称" name="title"> </div> <button type="submit" class="btn btn-primary">提交</button> </form> </div> <script src="{% static 'js/jquery.min.js' %}"></script> <script src="{% static 'js/bootstrap.min.js' %}"></script> </body> </html>
部门修改页面 bumen_edit.html
{% load static %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> 修改部门 </title> <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}"> </head> <body> <nav class="navbar navbar-default"> <div class="container-fluid"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">CMS</a> </div> <!-- Collect the nav links, forms, and other content for toggling --> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li class="active"><a href="/bumen/list/">部门 <span class="sr-only">(current)</span></a></li> <li><a href="/userinfo/list/">员工</a></li> </ul> <ul class="nav navbar-nav navbar-right"> <li><a href="#">admin</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">会员管理 <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><!-- /.navbar-collapse --> </div><!-- /.container-fluid --> </nav> <div class="container"> <div class="panel panel-default"> <div class="panel-heading"> <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span> 修改部门</div> </div> <form method="post"> {% csrf_token %} <div class="form-group"> <label for="exampleInputEmail1">部门名称</label> <input type="text" class="form-control" placeholder="部门名称" name="title" value="{{ bumenjieguo.title }}"> </div> <button type="submit" class="btn btn-primary">提交</button> </form> </div> <script src="{% static 'js/jquery.min.js' %}"></script> <script src="{% static 'js/bootstrap.min.js' %}"></script> </body> </html>
标签:bumen,title,request,list,信息,django,部门,html 来源: https://www.cnblogs.com/longly1111/p/16091933.html