The main organization is the hierarchy of rights management module, staff, it may be the company's organizational structure, division, team, and so on.
1 new organizational structure functions to achieve
1.1 Organizational Structure template page
Organizational structure template page, is to manage the organizational structure, organizational structure to add, change, delete, search operation when the page accessed by the user.
1, the new folder sandboxMP / templates / system / structure used to store all the templates organizational structure related
2, copy sandboxMP / templates / index.html to the directory structure just created, rename structure.html
3, open just copied structure.html file, add the contents of the code characters:
{% block content %} <!-- Main content --> <section class="content"> 组织架构页:文字部分是新增内容,这里是组织架构的基础页面,有关组织架构的操作查询都是在这里完成的。 </section> <!-- /.content --> {% endblock %}
1.2 Organization view basis
1, the new document sandboxMP / apps / system / views_structure.py, view all operations related to the organizational structure are defined in views_structure.py in.
2. Define the organizational structure in the base view views_structure.py file you just created:
from django.views.generic.base import TemplateView
from .mixin import LoginRequiredMixin
class StructureView(LoginRequiredMixin, TemplateView): template_name = 'system/structure/structure.html'
1.3 Organizational Structure Access URL
Modify sandboxMP / apps / system / urls.py, add the following elements:
from . import views_structure
app_name = 'system'
urlpatterns = [
'''原有内容省略' path('basic/structure/', views_structure.StructureView.as_view(), name='basic-structure'), ]
Run the project, you can access the organizational structure of the page http://127.0.0.1:8000/system/basic/structure/
2 add functions to achieve organizational structure
Add View 2.1 organizational structure
Adding functional view are implemented: adding a rendered page, the page is submitted to add data received and stored in the database
1, modified sandboxMP / apps / system / forms.py, add the following:
from .models import Structure
class StructureForm(forms.ModelForm): class Meta: model = Structure fields = ['type', 'name', 'parent']
2, modify sandboxMP / apps / system / views_structure.py, add the following:
import json
from django.views.generic.base import View
from django.shortcuts import render from django.shortcuts import HttpResponse from .models import Structure from .forms import StructureForm class StructureCreateView(LoginRequiredMixin, View): def get(self, request): ret = dict(structure_all=Structure.objects.all()) return render(request, 'system/structure/structure_create.html', ret) def post(self, request): res = dict(result=False) structure = Structure() structure_form = StructureForm(request.POST, instance=structure) if structure_form.is_valid(): structure_form.save() res['result'] = True return HttpResponse(json.dumps(res), content_type='application/json')
2.2 URL organizational structure to add functionality configuration:
Modify sandboxMP / apps / system / urls.py, add new content in urlpatterns in:
urlpatterns = [
'''原有内容省略'''
path('basic/structure/create/', views_structure.StructureCreateView.as_view(), name='basic-structure-create'),
]
2.3 Organizational Structure of the Add Page Templates
Add Page organization, is a pop page, inherited sandboxMP / templates / base_layer.html, the results page:
1, the new organizational structure in sandboxMP / templates / system / structure Contents Add template: structure_create.html
2, delete structure_create.html original content, add the following content:
{% extends 'base-layer.html' %} {% load staticfiles %} {% block css %} <link rel="stylesheet" href="{% static 'js/plugins/layer/skin/layer.css' %}"> {% endblock %} {% block main %} <div class="box box-danger"> <form class="form-horizontal" id="addForm" method="post"> {% csrf_token %} <div class="box-body"> <fieldset> <legend> <h4>组织架构信息</h4> </legend> <div class="form-group has-feedback"> <label class="col-sm-2 control-label">名称</label> <div class="col-sm-3"> <input class="form-control" name="name" type="text" /> </div> <label class="col-sm-2 control-label">类别</label> <div class="col-sm-3"> <select class="form-control" name="type"> <option value="unit">单位</option> <option value="department">部门</option> </select> </div> </div> <div class="form-group has-feedback"> <label class="col-sm-2 control-label">所属</label> <div class="col-sm-3"> <select class="form-control" name="parent"> <option></option> {% for stru in structure_all %} <option value={{ stru.id }}> {{ stru.name }} </option> {% endfor %} </select> </div> </div> </fieldset> </div> <div class="box-footer "> <div class="row span7 text-center "> <button type="button" id="btnCancel" class="btn btn-default margin-right ">重置</button> <button type="button" id="btnSave" class="btn btn-info margin-right ">保存</button> </div> </div> </form> </div> {% endblock %} {% block javascripts %} <script type="text/javascript"> $("#btnSave").click(function () { var data = $("#addForm").serialize(); $.ajax({ type: $("#addForm").attr('method'), url: "{% url 'system:basic-structure-create' %}", data: data, cache: false, success: function (msg) { if (msg.result) { layer.alert('