您现在的位置是: 网站首页 >Django >Django轮班排班管理系统 Django
【Schedule轮班排班】03.法定工作日·休息日管理模块
admin2018年11月28日 14:04 【Django | Html | JavaScript | Python 】 1773人已围观
Django轮班排班管理系统简介 基于内部业务需求,某些岗位需要进行工作日值班以及周六值班, 如果每次手动去制作Excel排班表,确实比较费时间,就考虑自动化生成的方式去做。 没有什么技术含量,可以了解下循环生成数据方法。我也没找到相关的代码参考,个人写的很烂,仅供参考。 环境要求:Django2.x.x
http://127.0.0.1:8000/duty/legalday/list/ ![BLOG_20181128_141745_97](/media/blog/images/2018/11/BLOG_20181128_141745_97.png "博客图集BLOG_20181128_141745_97.png") ![BLOG_20181128_141804_14](/media/blog/images/2018/11/BLOG_20181128_141804_14.png "博客图集BLOG_20181128_141804_14.png") ## 创建法定日期表单 表单为每个字段添加class样式属性 ```python from django import forms from .models import LegalDay, Employee class LegalDayForm(forms.ModelForm): class Meta: model = LegalDay 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] # 添加属性 filed_obj.widget.attrs.update({'class': 'form-control'}) return forms.ModelForm.__new__(cls) ``` ## 法定日期增删查改视图 使用基于类的视图 ```python from .forms import LegalDayForm class LegalDayList(ListView): model = LegalDay context_object_name = 'legal_day_list' template_name = 'legal-day-list.html' class LegalDayCreate(CreateView): model = LegalDay form_class = LegalDayForm template_name = 'legal-day-edit.html' success_url = reverse_lazy('duty:legal_day_create') class LegalDayUpdate(UpdateView): model = LegalDay form_class = LegalDayForm pk_url_kwarg = 'legal_day_id' template_name = 'legal-day-edit.html' success_url = reverse_lazy('duty:legal_day_list') class LegalDayDelete(DeleteView): model = LegalDay pk_url_kwarg = 'legal_day_id' template_name = 'legal-day-del.html' success_url = reverse_lazy('duty:legal_day_list') ``` ## 法定日期管理模板 ### 列表查看 创建legal-day-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> </tr> </thead> <tbody> {% for legal_day in legal_day_list %} <tr {% if legal_day.legal_type == 0 %} style="background-color:#CEFFCE;" {% elif legal_day.legal_type == 1 %} style="background-color:#FFE6D9;" {% endif %} > <td>{{ legal_day.date }}</td> <td>{{ legal_day.get_legal_type_display }}</td> <td> <a class="btn btn-primary btn-xs" href="{% url 'duty:legal_day_update' legal_day.id %}">更新</a> <a class="btn btn-danger btn-xs" href="{% url 'duty:legal_day_delete' legal_day.id %}">删除</a> </td> </tr> {% endfor %} </tbody> </table> <div> <a class="btn btn-primary btn-sm" href="{% url 'duty:legal_day_create' %}">添加</a> </div> </div> </div> {% endblock %} ``` ### 增加更新 使用同一个模板legal-day-edit.html,并且添加一个日期选择器js ```html {% extends 'base-duty.html' %} {% load static %} {% block css %} <link href="{% static 'hAdmin/css/plugins/datapicker/datepicker3.css' %}" rel="stylesheet"> {% 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>根据法定日期,将法定工作日和节假日都记录到表内</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:legal_day_list' %}">返回</a> </div> </form> </div> </div> {% endblock %} {% block js %} <script src="{% static 'hAdmin/js/plugins/datapicker/bootstrap-datepicker.js' %}"></script> <script> $('#id_date').datepicker({ language: "zh-CN", todayBtn: "linked", keyboardNavigation: false, forceParse: false, calendarWeeks: true, autoclose: true, todayHighlight: true, endDate: "{{ end_date }}", format: "yyyy-mm-dd", }); </script> {% endblock %} ``` ### 删除 创建legal-day-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:legal_day_list' %}">返回</a> </form> </div> </div> {% endblock %} ``` ![BLOG_20181128_142501_47](/media/blog/images/2018/11/BLOG_20181128_142501_47.png "博客图集BLOG_20181128_142501_47.png") ## 法定日期管理路由 在该app的urls.py中增加上面增删查改的路由 ```python from .views import ScheduleIndex, LegalDayList, LegalDayCreate, LegalDayUpdate, LegalDayDelete urlpatterns = [ path('legalday/list/', permission_required('usercenter.permission_user_manage', raise_exception=True)(LegalDayList.as_view()), name='legal_day_list'), # 显示法定日期 path('legalday/create/', permission_required('usercenter.permission_user_manage', raise_exception=True)(LegalDayCreate.as_view()), name='legal_day_create'), # 增加法定日期 path('legalday/update/<int:legal_day_id>/', permission_required('usercenter.permission_user_manage', raise_exception=True)(LegalDayUpdate.as_view()), name='legal_day_update'), # 更新法定日期 path('legalday/delete/<int:legal_day_id>/', permission_required('usercenter.permission_user_manage', raise_exception=True)(LegalDayDelete.as_view()), name='legal_day_delete'), # 删除法定日期 ] ```
很赞哦! (0)
相关文章
文章交流
- emoji