我有一个充满嵌套{%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 %}
谢谢!