Django4全栈进阶之路22 项目实战(三种方式开发部门管理):方式三:FBV+ModelForm+get_object_or_404
1、视图
@login_required def department_list_view(request): departments = Department.objects.all() return render(request, 'base/depart/department_list.html', {'departments': departments}) # department = DepartmentsForm() # return render(request, 'base/depart/department_list.html', {'department': department}) def department_detail_view(request, pk): department = get_object_or_404(Department, pk=pk) return render(request, 'base/depart/department_detail.html', {'department': department}) @login_required def department_create_view(request): if request.method == 'POST': form = DepartmentsForm(request.POST) if form.is_valid(): department = form.save(commit=False) department.created_by = request.user department.updated_by = request.user department.save() # return redirect('department_detail', pk=department.pk) return redirect('department_list') else: form = DepartmentsForm() return render(request, 'base/depart/department_create.html', {'form': form}) @login_required def department_edit_view(request, pk): department = get_object_or_404(Department, pk=pk) if request.method == 'POST': form = DepartmentsForm(request.POST, instance=department) if form.is_valid(): department = form.save(commit=False) department.updated_by = request.user # return redirect('department_detail', pk=department.pk) return redirect('department_list') else: form = DepartmentsForm(instance=department) return render(request, 'base/depart/department_edit.html', {'form': form}) @login_required def department_delete_View(request, pk): department = get_object_or_404(Department, pk=pk) if request.method == 'POST': department.delete() return redirect('department_list') return render(request, 'base/depart/department_delete.html', {'department': department})
2、路由
urlpatterns = [ # FBV模式+ ModelForm path('department/', views.department_list_view, name='department_list'), path('department/<int:pk>/', views.department_detail_view, name='department_detail'), path('department/add/', views.department_create_view, name='department_add'), path('department/<int:pk>/update/', views.department_edit_view, name="department_edit"), path('depart/delete/<int:pk>/', views.depart_delete_view, name='department_delete'), ]
3、模块
department_list.html
{% extends 'base.html' %} {% block content %} <div style="margin-top: 16px;"> <a href="{% url 'department_add' %}" class="btn btn-primary mb-3">添加部门</a> </div> <div class="card mt-3"> <div class="card-header">部门列表</div> <div class="card-body"> <table class="table table-bordered"> <thead> <tr> <th>ID</th> <th>部门名称</th> <th>父级部门</th> <th>显示排序</th> <th>负责人</th> <th>联系电话</th> <th>邮箱</th> <th>部门状态</th> <th>操作</th> </tr> </thead> <tbody> {% for department in departments %} <tr> <td>{{ department.id }}</td> <td>{{ department.name }}</td> <td>{{ department.parent }}</td> <td>{{ department.sort }}</td> <td>{{ department.owner }}</td> <td>{{ department.phone }}</td> <td>{{ department.email }}</td> <td>{{ department.status }}</td> <td> <a href="{% url 'department_detail' department.pk %}" class="btn btn-primary btn-sm">详情</a> <a href="{% url 'department_edit' department.pk %}" class="btn btn-success btn-sm">编辑</a> <a href="{% url 'department_delete' department.pk %}" class="btn btn-danger btn-sm">删除</a> </td> </tr> {% empty %} <tr> <td colspan="9" style="text-align: center;">No departments yet.</td> </tr> {% endfor %} </tbody> </table> {% endblock %} </div> </div>
department_create.html
{% block content %} {% include 'base/depart/department_form.html' with form=form button_text='新增' form_title="新增部门" %} {% endblock %}
department_update.html
{% block content %} {% include 'base/depart/department_form.html' with form=form button_text='保存' form_title='编辑部门' %} {% endblock %}
department_form.html
{% extends 'base.html' %} {% load widget_tweaks %} {% block content %} <div class="card mt-3"> <div class="card-header"> {{ form_title }} </div> <div class="card-body"> <form method="post"> {% csrf_token %} <div class="form-group"> <label for="{{ form.name.id_for_label }}">部门名称:</label> {{ form.name|attr:"class:form-control" }} </div> <div class="form-group"> <label for="{{ form.parent.id_for_label }}">父级部门:</label> {{ form.parent|attr:"class:form-control" }} </div> <div class="form-group"> <label for="{{ form.description.id_for_label }}">部门介绍:</label> {{ form.description|attr:"class:form-control" }} </div> <div class="form-group"> <label for="{{ form.sort.id_for_label }}">显示排序:</label> {{ form.sort|attr:"class:form-control" }} </div> <div class="form-group"> <label for="{{ form.owner.id_for_label }}">负责人:</label> {{ form.owner|attr:"class:form-control" }} </div> <div class="form-group"> <label for="{{ form.phone.id_for_label }}">联系电话:</label> {{ form.phone|attr:"class:form-control" }} </div> <div class="form-group"> <label for="{{ form.email.id_for_label }}">邮箱:</label> {{ form.email|attr:"class:form-control" }} </div> <div class="form-group"> <label for="{{ form.status.id_for_label }}">部门状态:</label> {{ form.status|attr:"class:form-control"}} </div> <button type="submit" class="btn btn-primary"> {{ button_text }}</button> </form> </div> </div> {% endblock %}
department_detail.html
{% extends 'base.html' %} {% block content %} <h1>{{ department.name }}</h1> <p>{{ department.parent.name }}</p> <p>{{ department.description }}</p> <p>{{ department.sort }}</p> <p>{{ department.owner }}</p> <p>{{ department.email }}</p> <p>{{ department.created_by.username }}</p> <p>{{ department.created_at }}</p> <p>{{ department.updated_by.username }}</p> <p>{{ department.updated_at }}</p> <a href="{% url 'department_edit' department.pk %}" class="btn btn-success btn-sm">编辑</a> <a href="{% url 'department_delete' department.pk %}" class="btn btn-danger btn-sm">删除</a> {% endblock %}
department_delete.html
{% extends 'base.html' %} {% block content %} <h1>Confirm delete</h1> <p>Are you sure you want to delete department "{{ department.name }}"?</p> <form method="post"> {% csrf_token %} <button type="submit" class="btn btn-danger">Yes, delete</button> <a href="{% url 'department_list' %}" class="btn btn-secondary">No, cancel</a> </form> {% endblock %}