Django 项目中展示表格

90 阅读1分钟

要在 Django 项目中展示表格,可以使用 Django 的模板系统来创建 HTML 表格。以下是一个简单的实现示例,包括视图、模板和 URL 配置。

1. 定义模型(Model)

假设你已经定义了 ProjectProjectNode 模型,如下所示:

from django.db import models

class Project(models.Model):
    name = models.CharField(max_length=100)
    cycle = models.IntegerField()
    progress = models.TextField(blank=True)
    start_date = models.DateField()
    end_date = models.DateField()
    creator = models.CharField(max_length=100)
    manager = models.CharField(max_length=100)
    report_issue = models.TextField(blank=True)

    def __str__(self):
        return self.name

class ProjectNode(models.Model):
    project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='nodes')
    node_name = models.CharField(max_length=100)
    node_start_date = models.DateField()
    node_end_date = models.DateField()
    manager = models.CharField(max_length=100)
    report_issue = models.TextField(blank=True)

    def __str__(self):
        return f"{self.project.name} - {self.node_name}"

2. 编写视图(View)

views.py 文件中,创建视图来获取这些数据:

from django.shortcuts import render
from .models import Project, ProjectNode

def project_list(request):
    projects = Project.objects.all()
    return render(request, 'project_list.html', {'projects': projects})

def project_nodes(request, project_id):
    project = Project.objects.get(id=project_id)
    nodes = project.nodes.all()
    return render(request, 'project_nodes.html', {'project': project, 'nodes': nodes})

3. 配置 URL(URLs)

urls.py 中配置 URL 路由:

from django.urls import path
from . import views

urlpatterns = [
    path('projects/', views.project_list, name='project_list'),
    path('projects/<int:project_id>/nodes/', views.project_nodes, name='project_nodes'),
]

4. 创建模板(Templates)

project_list.html - 显示项目表格:

<!DOCTYPE html>
<html>
<head>
    <title>项目列表</title>
</head>
<body>
    <h1>项目列表</h1>
    <table border="1">
        <thead>
            <tr>
                <th>项目名称</th>
                <th>周期</th>
                <th>进度</th>
                <th>项目开始时间</th>
                <th>项目结束时间</th>
                <th>创建人</th>
                <th>负责人</th>
                <th>报告问题</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            {% for project in projects %}
            <tr>
                <td>{{ project.name }}</td>
                <td>{{ project.cycle }}</td>
                <td>{{ project.progress }}</td>
                <td>{{ project.start_date }}</td>
                <td>{{ project.end_date }}</td>
                <td>{{ project.creator }}</td>
                <td>{{ project.manager }}</td>
                <td>{{ project.report_issue }}</td>
                <td><a href="{% url 'project_nodes' project.id %}">查看节点</a></td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</body>
</html>

project_nodes.html - 显示项目节点表格:

<!DOCTYPE html>
<html>
<head>
    <title>项目节点</title>
</head>
<body>
    <h1>项目节点 - {{ project.name }}</h1>
    <table border="1">
        <thead>
            <tr>
                <th>节点名称</th>
                <th>节点开始时间</th>
                <th>节点结束时间</th>
                <th>负责人</th>
                <th>报告问题</th>
            </tr>
        </thead>
        <tbody>
            {% for node in nodes %}
            <tr>
                <td>{{ node.node_name }}</td>
                <td>{{ node.node_start_date }}</td>
                <td>{{ node.node_end_date }}</td>
                <td>{{ node.manager }}</td>
                <td>{{ node.report_issue }}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
    <a href="{% url 'project_list' %}">返回项目列表</a>
</body>
</html>

总结

  • views.py: 定义了两个视图来处理项目列表和项目节点的展示。
  • urls.py: 配置了 URL 路由。
  • 模板文件:
    • project_list.html 用于显示项目表格。
    • project_nodes.html 用于显示项目节点表格。

这样就可以在前端展示你设计的表格了