如何在{%include%}标签django中包含我的论点?

时间:2019-12-30 06:21:01

标签: python django django-templates django-views

    {% if type == "Cryptography" %}
        {% include 'Cryptography/_____.html' %}
    {% elif type == "Password Cracking" %}
        {% include 'PasswordCracking/_____.html' %}
    {% endif %}

在模板中,我想在C和PC目录中包含一些带有1.html 2.html 3.html的HTML页面。我曾尝试过这样的操作,其中页面是我的参数变量。

    {% if type == "Cryptography" %}
        {% include 'cryptography/'{{page}}'html' %}
    {% elif type == "Password Cracking" %}
        {% include 'PasswordCracking/'{{page}}'.html' %}
    {% endif %}

查看此

def lessons(request, foo, page):
    return render(request, 'lessons.html', {'type': foo, 'page': page})

注意:类型和页面是我的论点

1 个答案:

答案 0 :(得分:1)

您可以使用add过滤器来连接字符串

{% if type == "Cryptography" %}
    {% include "cryptography/"|add:page|add:".html" %}
{% elif type == "Password Cracking" %}
    {% include "PasswordCracking/"|add:page|add:".html" %}
{% endif %}