我想生成一个网站的主页,其中包含不同类型的各个部分。有一个关于我们的部分,最近的项目部分等。我的目标是将视图逻辑和模板渲染留给组成这个项目的各个应用程序(关于,组合等)。我目前的解决方案包括动态地构造一个Django模板,然后使其像这样:我可以使用django模板呈现django模板吗?
在MYAPP/templatetags /myapp_tags.py:
@register.simple_tag
def render_sections(sections):
template = ""
for section in sections:
if section.type == "about":
template += "{% load about %}"
template += "{% render_about_section %}"
elif section.type == "portfolio":
template += "{% load portfolio %}"
template += "{% render_portfolio_section %}"
return render_to_string(template)
模板/ index.html的:
{% load myapp_tags %}
{% block content %}
{% render_sections %}
{% endblock %}
由于我已经在构建一个模板,我想知道:为什么我不能使用django的模板系统来呈现django模板?可以替换上述代码的示例模板是:
{% for s in sections %}
{% load {{s.type}} %}
{% render_{{s.type}}_section %}}
{% endfor %}
我能做些什么来完成这项工作?有没有更好的方法来呈现异构项目列表?