凯发k8天生赢家一触即发

django4全栈进阶之路23 项目实战(报修类型表):应用程序命名空间app-凯发k8天生赢家一触即发

2023-08-17,,

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//update/', views.repairtypeupdateview.as_view(), name='repair_type_update'),
path('repair_types//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 %}



维修类型列表











{# #}
{# #}
{# #}
{# #}




{% for repair_type in repair_types %}





{# #}
{# #}
{# #}
{# #}


{% endfor %}

序号 归属部门 类型名称 类型父级创建账号创建时间修改账号最后修改时间 操作
{{ repair_type.id }} {{ repair_type.department }} {{ repair_type.name }} {{ repair_type.parent.name }}{{ repair_type.created_by }}{{ repair_type.created_time }}{{ repair_type.modified_by }}{{ repair_type.modified_time }}




            



{% 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 %}


{{ form_title }}



{% csrf_token %}


{{ form.name|attr:"class:form-control" }}



{{ form.parent|attr:"class:form-control" }}



{{ form.department|attr:"class:form-control" }}






{% endblock %}

confirm_delete.html

{% block content %}








are you sure you want to delete "{{ object }}"?







{% csrf_token %}





{% endblock %}

django4全栈进阶之路23 项目实战(报修类型表):应用程序命名空间app_name和分页组件paginator的相关教程结束。

网站地图