您现在的位置是: 网站首页 >Django >Django轮班排班管理系统 Django
【Schedule轮班排班】04.值班成员管理模块
admin2018年11月28日 14:28 【Django | Html | JavaScript | Python 】 1556人已围观
Django轮班排班管理系统简介 基于内部业务需求,某些岗位需要进行工作日值班以及周六值班, 如果每次手动去制作Excel排班表,确实比较费时间,就考虑自动化生成的方式去做。 没有什么技术含量,可以了解下循环生成数据方法。我也没找到相关的代码参考,个人写的很烂,仅供参考。 环境要求:Django2.x.x
http://127.0.0.1:8000/duty/employee/list/ ![BLOG_20181128_143022_52](/media/blog/images/2018/11/BLOG_20181128_143022_52.png "博客图集BLOG_20181128_143022_52.png") ![BLOG_20181128_143052_12](/media/blog/images/2018/11/BLOG_20181128_143052_12.png "博客图集BLOG_20181128_143052_12.png") ## 创建值班成员表单 修改forms.py增加,为每个字段添加class样式,由于有复选框,需要排除掉 ```python from .models import Employee class EmployeeForm(forms.ModelForm): class Meta: model = Employee fields = '__all__' # django是通过“__new__”方法,找到ModelForm里面的每个字段的,然后循环出每个字段添加自定义样式 def __new__(cls, *args, **kwargs): # cls.base_fields是一个元祖,里面是 所有的 【(字段名,字段的对象),(),()】 for field_name in cls.base_fields: filed_obj = cls.base_fields[field_name] if field_name != 'available': # 添加属性,复选框不添加样式 filed_obj.widget.attrs.update({'class': 'form-control'}) return forms.ModelForm.__new__(cls) ``` ## 值班成员增删查改视图 基于类的视图 ```python class EmployeeList(ListView): model = Employee context_object_name = 'employee_list' template_name = 'employee-list.html' class EmployeeCreate(CreateView): model = Employee form_class = EmployeeForm template_name = 'employee-edit.html' success_url = reverse_lazy('duty:employee_create') class EmployeeUpdate(UpdateView): model = Employee form_class = EmployeeForm pk_url_kwarg = 'employee_id' template_name = 'employee-edit.html' success_url = reverse_lazy('duty:employee_list') class EmployeeDelete(DeleteView): model = Employee pk_url_kwarg = 'employee_id' template_name = 'employee-del.html' success_url = reverse_lazy('duty:employee_list') ``` ## 成员管理模板 ### 列表展示 添加employee-list.html模板 ```html {% extends 'base-duty.html' %} {% block title %}{{ block.super }} - 值班成员管理{% endblock %} {% block breadcrumb %} <li> <a href="/">主页</a> </li> <li> <strong>值班成员</strong> </li> {% endblock %} {% block content %} <div class="ibox float-e-margins"> <div class="ibox-title"> <h5>值班成员</h5> </div> <div class="ibox-content"> <table class="table table-striped"> <thead> <tr> <th>名字</th> <th>编号</th> <th>是否排班</th> <th>操作</th> </tr> </thead> <tbody> {% for employee in employee_list %} <tr> <td>{{ employee.name }}</td> <td>{{ employee.num }}</td> <td>{% if employee.available %}是{% else %}-{% endif %}</td> <td> <a class="btn btn-primary btn-xs" href="{% url 'duty:employee_update' employee.id %}">更新</a> <a class="btn btn-danger btn-xs" href="{% url 'duty:employee_delete' employee.id %}">删除</a> </td> </tr> {% endfor %} </tbody> </table> <div> <a class="btn btn-primary btn-sm" href="{% url 'duty:employee_create' %}">添加</a> </div> </div> </div> {% endblock %} ``` ### 成员增加更新模板 增加employee-edit.html,用于成员的更新和增加 ```html {% extends 'base-duty.html' %} {% load static %} {% block css %} {% endblock %} {% block title %}{{ block.super }} - 值班成员管理{% endblock %} {% block breadcrumb %} <li> <a href="/">主页</a> </li> <li> <strong>值班成员</strong> </li> {% endblock %} {% block content %} <div class="ibox float-e-margins"> <div class="ibox-title"> <h5>值班成员</h5> </div> <div class="ibox-content"> <p>编辑值班成员,编号需唯一,且顺序一致,不得有空缺(SB后台没判断)</p> <form method="post" autocomplete="off"> {{ form.as_p }} {% csrf_token %} <div class="form-group"> <button class="btn btn-primary" type="submit">提交</button> <a class="btn btn-success" href="{% url 'duty:employee_list' %}">返回</a> </div> </form> </div> </div> {% endblock %} {% block js %} {% endblock %} ``` ### 成员删除 增加employee-del.html成员删除模板 ```html {% extends 'base-duty.html' %} {% block title %}{{ block.super }} - 值班成员管理{% endblock %} {% block breadcrumb %} <li> <a href="/">主页</a> </li> <li> <strong>值班成员</strong> </li> {% endblock %} {% block content %} <div class="ibox float-e-margins"> <div class="ibox-title"> <h5>值班成员</h5> </div> <div class="ibox-content"> <form method="post"> {% csrf_token %} <p>确认删除:{{ object }}</p> <input class="btn btn-danger btn-sm" type="submit" value="确认删除"> <a class="btn btn-success btn-sm" href="{% url 'duty:employee_list' %}">返回</a> </form> </div> </div> {% endblock %} ``` ![BLOG_20181128_143528_14](/media/blog/images/2018/11/BLOG_20181128_143528_14.png "博客图集BLOG_20181128_143528_14.png") ## 成员管理路由 修改urls.py增加成员管理路由 ```python from .views import EmployeeList, EmployeeCreate, EmployeeDelete, EmployeeUpdate urlpatterns = [ path('employee/list/', permission_required('usercenter.permission_user_manage', raise_exception=True)(EmployeeList.as_view()), name='employee_list'), # 显示值班成员 path('employee/create/', permission_required('usercenter.permission_user_manage', raise_exception=True)(EmployeeCreate.as_view()), name='employee_create'), # 增加值班成员 path('employee/update/<int:employee_id>/', permission_required('usercenter.permission_user_manage', raise_exception=True)(EmployeeUpdate.as_view()), name='employee_update'), # 更新值班成员 path('employee/delete/<int:employee_id>/', permission_required('usercenter.permission_user_manage', raise_exception=True)(EmployeeDelete.as_view()), name='employee_delete'), # 删除值班成员 ] ```
很赞哦! (1)
相关文章
文章交流
- emoji