Django的html在for遍历后显示“一、二、三...”和“1,2,3...”分级标题

例如当天的html为:

html 复制代码
{% load static %}
{% csrf_token %}
<!DOCTYPE html>
<html>
<head>
    <title>生活规划师</title>
    <link rel="stylesheet" href="{% static 'css/LifePlanningGuide.css' %}">
    <script src="{% static 'js/LifePlanningGuide.js' %}"></script>
</head>
<body>
    <h1>生活规划师</h1>
    <button id="create-theme-button">新建主题</button>
        
    {% for theme in themes %}
        <div class="theme-container">
          <h2 class="theme-title">{{ theme.name }}</h2>
          <button id="create-task-button-{{ theme.id }}" class="create-task-button" data-theme-id="{{ theme.id }}">新建任务</button>
    
          {% for task in tasks %}
              {% if task.theme.id == theme.id %}
                  <div class="task-container">
                      <p class="task-description">{{ task.description }}</p>
                      <div class="task-buttons">
                          <button class="top-task-button" data-task-id="{{ task.id }}">置顶</button>
                          <button class="move-up-task-button" data-task-id="{{ task.id }}">上移</button>
                          <button class="move-down-task-button" data-task-id="{{ task.id }}">下移</button>
                          <button class="delete-task-button" data-task-id="{{ task.id }}">删除</button>
                      </div>
                  </div>
              {% endif %}
          {% endfor %}
        </div>
    {% endfor %}
</body>
</html>

我们希望在每个分级标题前面显示标题号,怎么实现呢?

首先看看我的view.py里面的视图函数:

python 复制代码
def index(request):
    themes = Theme.objects.filter(user=request.user, is_deleted=False)
    tasks = Task.objects.filter(theme__user=request.user, theme__is_deleted=False).order_by('-is_top', 'created_at')
    return render(request, 'LifePlanningGuide.html', {'themes': themes, 'tasks': tasks})

鉴于现有视图函数,我们需要稍微调整之前提出的方案,以便在模板中实现每个主题的中文数字编号和每个任务的阿拉伯数字编号。由于是分开传递themestasks,而非作为主题属性的任务列表,我们可以在模板中使用一些小技巧来实现需求。

首先,定义一个简单的数字到中文的转换函数可能不太适用于当前的情况,因为Django的模板不支持在模板中直接执行Python代码。但是,我们可以通过自定义模板过滤器来实现。

步骤 1: 创建一个自定义模板过滤器

  1. 在Django应用目录中创建一个templatetags目录。
  2. templatetags目录中,创建一个__init__.py文件和一个custom_filters.py(或其他你喜欢的名字)文件。
  3. custom_filters.py中,定义一个转换数字到中文的过滤器和一个转换数字到阿拉伯数字的过滤器(虽然后者在Django模板中通过forloop.counter已有内置支持,这里仅为演示自定义过滤器的创建)。
python 复制代码
from django import template

register = template.Library()

@register.filter(name='num_to_chinese')
def num_to_chinese(value):
    chinese_nums = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十']
    if value < 11:
        return chinese_nums[value]
    elif value < 20:
        return '十' + chinese_nums[value - 10]
    else:
        return chinese_nums[value // 10] + '十' + (chinese_nums[value % 10] if value % 10 != 0 else '')

步骤 2: 在视图中按主题分组任务

调整视图,以在每个主题下分组任务,这样可以在模板中更容易地管理。

python 复制代码
from django.shortcuts import render
from .models import Theme, Task

def index(request):
    themes = Theme.objects.filter(user=request.user, is_deleted=False)
    tasks = Task.objects.filter(theme__user=request.user, theme__is_deleted=False).order_by('-is_top', 'created_at')
    tasks_grouped_by_theme = {
        theme.id: list(Task.objects.filter(theme=theme, theme__is_deleted=False).order_by('-is_top', 'created_at'))
        for theme in themes
    }
    return render(request, 'LifePlanningGuide.html', {'themes': themes, 'tasks_grouped_by_theme': tasks_grouped_by_theme, 'tasks': tasks})

步骤 3: 在HTML模板使用自定义过滤器和调整数据展示

首先,确保在模板顶部加载自定义的模板标签:

html 复制代码
{% load static %}
{% csrf_token %}
{% load custom_filters %}

然后,根据主题和任务展示的需求调整HTML模板:

html 复制代码
{% load static %}
{% csrf_token %}
{% load custom_filters %}

<!DOCTYPE html>
<html>
<head>
    <title>生活规划师</title>
    <link rel="stylesheet" href="{% static 'css/LifePlanningGuide.css' %}">
    <script src="{% static 'js/LifePlanningGuide.js' %}"></script>
</head>
<body>
    <h1>生活规划师</h1>
    <button id="create-theme-button">新建主题</button>
        
    {% for theme in themes %}
        <div class="theme-container">
          <h2 class="theme-title">{{ forloop.counter|num_to_chinese }}、{{ theme.name }}</h2>
          <button id="create-task-button-{{ theme.id }}" class="create-task-button" data-theme-id="{{ theme.id }}">新建任务</button>
    
          {% for task in tasks %}
              {% if task.theme.id == theme.id %}
                  <div class="task-container">
                      <p class="task-description">{{ forloop.counter }}. {{ task.description }}</p>
                      <div class="task-buttons">
                          <button class="top-task-button" data-task-id="{{ task.id }}">置顶</button>
                          <button class="move-up-task-button" data-task-id="{{ task.id }}">上移</button>
                          <button class="move-down-task-button" data-task-id="{{ task.id }}">下移</button>
                          <button class="delete-task-button" data-task-id="{{ task.id }}">删除</button>
                      </div>
                  </div>
              {% endif %}
          {% endfor %}
        </div>
    {% endfor %}
</body>
</html>

通过上述步骤,实现主题标题前以中文数字编号,每个主题的子任务前以阿拉伯数字编号的需求。

相关推荐
IT毕设梦工厂1 小时前
计算机毕业设计选题推荐-在线拍卖系统-Java/Python项目实战
java·spring boot·python·django·毕业设计·源码·课程设计
哆木5 小时前
部署在线GBA游戏,并通过docker安装启动
游戏·html·gba
少年负剑去6 小时前
django分发路由
数据库·django·sqlite
丶重明7 小时前
【2024】前端学习笔记3-外部链接-内部链接-锚点链接
html
计算机学姐8 小时前
基于python+django+vue的影视推荐系统
开发语言·vue.js·后端·python·mysql·django·intellij-idea
小ᶻᶻᶻ9 小时前
HTML 和 CSS
html
软件技术NINI9 小时前
html知识点框架
前端·html
计算机学姐11 小时前
基于python+django+vue的家居全屋定制系统
开发语言·vue.js·后端·python·django·numpy·web3.py
6230_13 小时前
git使用“保姆级”教程1——简介及配置项设置
前端·git·学习·html·web3·学习方法·改行学it
加勒比海涛13 小时前
HTML 揭秘:HTML 编码快速入门
前端·html