我有一个充满嵌套 {% regroup %} 标签的 Django 模板,我觉得这是它如此缓慢的原因之一。而且,这样维护非常复杂。你有什么建议和设计模式可以让它更快、更可持续吗?我愿意接受任何建议,包括将逻辑移动到模型或创建模板标签......
这是一个代码片段,只有结构,让您体验一下它的样子......
{% extends "base.tex" %}
{% block content %}
{% for courseoutline in courseoutline_list %}
...
{% regroup courseoutline.coursemembership_set_by_teaching_unit by teaching_unit as course_list %}
{% for course in course_list %}
{% regroup course.list|dictsort:"course.title" by course.title as course_sublist %}
{% for course in course_sublist %}
{% with course.list|semester:1 as cc %}
...
{% for c in cc %}
...
{% endfor %}
{% endwith %}
{% with course.list|semester:2 as cc %}
{% for c in cc %}
{% endfor %}
{% endwith %}
{% endfor %}
{% endfor %}
{% regroup courseoutline.coursemembership_set_by_semester by semester as semester_list %}
{% for semester in semester_list %}
{% regroup semester.list by teaching_unit as ue_list %}
{% for ue in ue_list %}
{% for course in ue.list|dictsort:"code" %}
{% endfor %}
{% endfor %}
{% endfor %}
{% endfor %}
{% endblock %}
谢谢!