Swaroop Maddu
Swaroop Maddu

Reputation: 4854

How can I include my argument in {% include %} tag django?

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

In a template, I want to include some HTML pages with 1.html 2.html 3.html which are inside C and PC directories. I have tried like this where the page is my argument variable.

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

View for this

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

Note: type and page are my arguments

Upvotes: 0

Views: 86

Answers (1)

Iain Shelvington
Iain Shelvington

Reputation: 32244

You can use the add filter to concatenate strings

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

Upvotes: 1

Related Questions