1、应用程序命名空间app_name
from django.urls import path from . import views from .views import RepairDetailView app_name = 'repair' urlpatterns = [ path('repair_types/', views.RepairTypeListView.as_view(), name='repair_type_list'), path('repair_types/create/', views.RepairTypeCreateView.as_view(), name='repair_type_create'), path('repair_types/<int:pk>/update/', views.RepairTypeUpdateView.as_view(), name='repair_type_update'), path('repair_types/<int:pk>/delete/', views.RepairTypeDeleteView.as_view(), name='repair_type_delete'),]
2、模型
from django.db import models from BaseApp.models import Department from HRApp.models import Employee from MyProject import settings import uuid class RepairType(models.Model): department = models.ForeignKey(Department, verbose_name="部门", on_delete=models.CASCADE, to_field='id', related_name='RepairType_department') name = models.CharField(verbose_name="类型", max_length=100) parent = models.ForeignKey('self', verbose_name="父级", null=True, blank=True, on_delete=models.CASCADE, related_name='RepairType_parent') created_by = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name="创建账户", on_delete=models.CASCADE, related_name='repair_types_created') created_at = models.DateTimeField("创建时间", auto_now_add=True) updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name="修改账户", on_delete=models.CASCADE, related_name='repair_types_update') updated_at = models.DateTimeField("修改时间", auto_now=True) def __str__(self): return self.name class Meta: verbose_name = "报修类型" verbose_name_plural = "报修类型" ordering = ['department', 'name']
3、表单
from django import forms from .models import Department, RepairType, Repair class RepairTypeForm(forms.ModelForm): class Meta: model = RepairType # fields = '__all__' fields = ['name', 'parent', 'department'] parent = forms.ModelChoiceField(queryset=RepairType.objects.all().order_by('name'), required=False)
4、模板
list.html
{% extends 'base.html' %} {% block content %} <div class="container-fluid mt-3"> <div class="row"> <div class="col-md-12"> <h3 class="mb-3">维修类型列表</h3> <a href="{% url 'repair:repair_type_create' %}" class="btn btn-primary mb-2">新增</a> <div class="table-responsive"> <table class="table table-striped table-hover"> <thead> <tr> <th>序号</th> <th>归属部门</th> <th>类型名称</th> <th>类型父级</th> {# <th>创建账号</th>#} {# <th>创建时间</th>#} {# <th>修改账号</th>#} {# <th>最后修改时间</th>#} <th>操作</th> </tr> </thead> <tbody> {% for repair_type in repair_types %} <tr> <td>{{ repair_type.id }}</td> <td>{{ repair_type.department }}</td> <td>{{ repair_type.name }}</td> <td>{{ repair_type.parent.name }}</td> {# <td>{{ repair_type.created_by }}</td>#} {# <td>{{ repair_type.created_time }}</td>#} {# <td>{{ repair_type.modified_by }}</td>#} {# <td>{{ repair_type.modified_time }}</td>#} <td> <a href="{% url 'repair:repair_type_update' repair_type.id %}" class="btn btn-success btn-sm">编辑</a> <a href="{% url 'repair:repair_type_delete' repair_type.id %}" class="btn btn-danger btn-sm">删除</a> </td> </tr> {% endfor %} </tbody> </table> </div> <nav aria-label="Page navigation"> <ul> {% for article in articles %} <li> {{ article.title }} </li> {% endfor %} </ul> <ul class="pagination"> <!--#上一页--> <!--当有上一页时--> {% if page_obj.has_previous %} <li><a href="{% url 'repair:repair_type_list' %}?page={{ page_obj.previous_page_number }}">上一页</a> </li> {% else %} <li class="disabled"><a href="javascript:void(0)">上一页</a></li> {% endif %} <!--中间的页码,如果是当前页,添加active属性--> {% for page in paginator.page_range %} {% if page == page_obj.number %} <li class="active"> <a href="{% url 'repair:repair_type_list' %}?page={{ page }}">{{ page }}</a> </li> {% else %} <li><a href="{% url 'repair:repair_type_list' %}?page={{ page }}">{{ page }}</a></li> {% endif %} {% endfor %} <!--#下一页--> {% if page_obj.has_next %} <li> <a href="{% url 'repair:repair_type_list' %}?page={{ page_obj.next_page_number }}">下一页</a> </li> {% else %} <li class="disabled"><a href="javascript:void(0)">下一页</a></li> {% endif %} </ul> </nav>
</div>
</div>
</div>
{% endblock %}
create.html
{% block content %} {% include 'repair/repair_type/form.html' with form=form button_text='新增' form_title="新增报修类型" %} {% endblock %}
edit.html
{% block content %} {% include 'repair/repair_type/form.html' with form=form button_text='保存' form_title='编辑报修类型' %} {% endblock %}
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.department.id_for_label }}">责任部门:</label> {{ form.department|attr:"class:form-control" }} </div> <button type="submit" class="btn btn-primary"> {{ button_text }}</button> <a href="{% url 'repair:repair_type_list' %}" class="btn btn-secondary mr-2"> <i class="fas fa-arrow-left"></i> 返回 </a> </form> </div> </div> {% endblock %}
confirm_delete.html
{% block content %} <div class="container"> <div class="row"> <div class="col-md-12"> <h1>Delete Repair Type</h1> </div> </div> <div class="row"> <div class="col-md-12"> <p>Are you sure you want to delete "{{ object }}"?</p> </div> </div> <div class="row"> <div class="col-md-12"> <form method="post"> {% csrf_token %} <div class="modal-footer"> <a href="{% url 'repair:repair_type_list' %}" class="btn btn-secondary">Cancel</a> <button type="submit" class="btn btn-danger">Delete</button> </div> </form> </div> </div> </div> {% endblock %}
标签:paginator,repair,进阶,form,Django4,models,import,type,name From: https://www.cnblogs.com/beichengshiqiao/p/17364896.html