我想通过 Django 中的多个包含标签传递“上下文”变量,如下所示:
base.html:
{% load extras %}
{% table_of_contents course %}
目录表.html:
{% load extras %}
<h1>Table of contents</h1>
{% for section in course.sections %}
{% display_section section %}
{% endfor %}
额外的.py:
@register.inclusion_tag('table-of-contents.html', takes_context=True)
def table_of_contents(context, course):
return {
'course': course,
}
@register.inclusion_tag('display_section.html', takes_context=True)
def section_expanded(context, section):
# Identify the user from the context request
user = context['request'].user
return {
'section': section,
'completed': section.has_been_completed_by(user),
'outstanding_modules': section.get_outstanding_modules_for(user)
}
但是,当我运行上面的代码时,我得到一个关键错误,因为上下文变量没有传递到第二个包含标记:
KeyError at /courses/pivottables-video-course/table-of-contents/
'request'
如何确保上下文变量在传递到多个嵌套包含标记时仍然存在?